Listview in virtual Mode sorting in C-Sharp

What you will do when you have to display very large no(50000+) of records in listview? Because it will be slow down your application while scrolling and doing updation. To avoid these kind of situation .Net ListView has special feature called “Vitual Mode.” When you will display your data in virtual mode it will be fast because all list view items are not added to listview Items collection at a time and it will process only visible items of list view. For using Listview in virtual mode in c#. Please do following things.

For complete project please download from here. Sorting.ListView.VirtualMode.zip (10.74 kb)

Set ListView VirtualMode property true either from property bag or this.listView1.VirtualMode = true; and create event handler RetrieveVirtualItem.

private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)

        {

            if (e.ItemIndex >= this.m_ListItemCache.Count)

            {

                e.Item = new ListViewItem(“”);

                e.Item.SubItems.Add(“”);

                e.Item.SubItems.Add(“”);

            }

            else

            {

// You can directly create ListViewItem here from data using

// e.ItemIndex , but i have create earlier and using it here

                e.Item = m_ListItemCache[e.ItemIndex];

            }

        }

For sorting List View Item while list view in virtual mode is very easy. Just depending upon clicked column sort you data it will be displayed sorted.

Here i am giving example when my ListViewItem list is already created.

     public partial class Form1 : Form

    {

        //List to hold list view Item Cache.

        List<ListViewItem> m_ListItemCache = new List<ListViewItem>();

       // ListViewItem, IComparer<ListViewItem> class to sort list view item

        ListViewColumnSorter m_lvwColumnSorter = new ListViewColumnSorter();

        public Form1()

        {

            InitializeComponent();

           //Initialize list view and create list of list view items

            InitializeListView();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            this.listView1.VirtualListSize = this.m_ListItemCache.Count;

        }

        private void InitializeListView()

        {

            this.listView1.View = View.Details;//Set Detail view

            this.listView1.VirtualMode = true;//Set virtual mode true

    //Create ListView Columns

            ColumnHeader header = new ColumnHeader(“Name”);

            header.Text = “Name”;

            this.listView1.Columns.Add(header);

            header = new ColumnHeader(“Age”);

            header.Text = “Age”;

            this.listView1.Columns.Add(header);

            header = new ColumnHeader(“DOB”);

            header.Text = “DOB”;

            this.listView1.Columns.Add(header);

           //Create ListViteItem list for 50000 records

            m_ListItemCache = CreateTestDataList(50000);

        }

//Method to generate test data.

        private List<ListViewItem> CreateTestDataList(int count)

        {

            List<ListViewItem> list = new List<ListViewItem>();

            Random rnd = new Random();

            for (int i = 0; i < count; i++)

            {

                ListViewItem p = new ListViewItem(Guid.NewGuid().ToString());

                p.Tag = p.Text;

                int age = rnd.Next(10, 100);

                p.SubItems.Add(age.ToString());

                p.SubItems[0].Tag = age;

                DateTime dob = DateTime.Now.AddYears(-age);

                p.SubItems.Add(dob.ToString());

                p.SubItems[1].Tag = dob;

                list.Add(p);

            }

            return list;

        }

        private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)

        {

            if (e.ItemIndex >= this.m_ListItemCache.Count)

            {

//If Item index is out of range then create item with empty records

                e.Item = new ListViewItem(“”);

                e.Item.SubItems.Add(“”);

                e.Item.SubItems.Add(“”);

            }

            else

            {

                e.Item = m_ListItemCache[e.ItemIndex];//Show item

            }

        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)

        {

            // Determine if clicked column is already the column that is being sorted.

            if (e.Column == m_lvwColumnSorter.SortColumn)

            {

                // Reverse the current sort direction for this column.

                if (m_lvwColumnSorter.Order == SortOrder.Ascending)

                {

                    m_lvwColumnSorter.Order = SortOrder.Descending;

                }

                else

                {

                    m_lvwColumnSorter.Order = SortOrder.Ascending;

                }

            }

            else

            {

                // Set the column number that is to be sorted; default to ascending.

                m_lvwColumnSorter.SortColumn = e.Column;

                m_lvwColumnSorter.Order = SortOrder.Ascending;

            }

//Sort LitViewItem list depend upon column

            this.m_ListItemCache.Sort(m_lvwColumnSorter);

            this.listView1.VirtualListSize = 0;

            this.listView1.Update();

            this.listView1.VirtualListSize = this.m_ListItemCache.Count;

            this.listView1.Update();

        }

    }

For getting selected selected item in virtual mode use ItemSelectionChanged event and for multiple item selection use VirtualItemsSelectionRangeChanged event.

private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)

        {

            if (e.IsSelected)

            {

                ListViewItem selitem = e.Item;

            }

        }

 private void listView1_VirtualItemsSelectionRangeChanged(object sender, ListViewVirtualItemsSelectionRangeChangedEventArgs e)

        {

        }

For complete project please download from here. Sorting.ListView.VirtualMode.zip (10.74 kb)

Leave a Reply

Your email address will not be published. Required fields are marked *