WinForm ListView Virtual Mode load data to increase loading speed

Source: Internet
Author: User

  Setting the VirtualMode property to true places the ListView in virtual mode. The control no longer uses Collection.add () to add data, instead of using Retrievevirtualitem (occurs when the ListView was in virtual mode and requires a Li Stviewitem.) and Cachevirtualitems two events, using Retrievevirtualitem alone can also, Cachevirtualitems this event is mainly to facilitate the programmer to manipulate the buffer set, its parameters Cachevirtualitems EventArgs has a startindex and endindex two properties in virtual mode.

 In virtual mode, the performance can be greatly improved by getting the required data from the buffer to load. In other cases, it may be necessary to recalculate the value of the ListViewItem object frequently, and doing this for the entire collection will produce unacceptable performance.

Example code:

  

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Windows.Forms;4 5 namespacewinformtest6 {7      Public Partial classForm1:form8     {9         PrivateList<listviewitem>Mycache;Ten          PublicForm1 () One         { A InitializeComponent (); -  -Mycache =NewList<listviewitem>(); the         } -  -         Private voidForm1_Load (Objectsender, EventArgs e) -         { +Listview1.view =View.Details; -Listview1.virtualmode =true; +  AListview1.retrievevirtualitem + =NewRetrievevirtualitemeventhandler (listview1_retrievevirtualitem); at  -         } -  -         voidListview1_retrievevirtualitem (Objectsender, Retrievevirtualitemeventargs e) -         { -             if(Mycache! =NULL ) in             { -E.item =Mycache[e.itemindex]; to             } +             Else -             { the                 //a cache miss, so create A new ListViewItem and pass it back. *                 intx = E.itemindex *E.itemindex; $E.item =NewListViewItem (x.tostring ());Panax Notoginseng             } -         } the          +         Private voidButton1_Click (Objectsender, EventArgs e) A         { thelist<student> list =getstudentlist (); +             foreach(varIteminchlist) -             { $ListViewItem ListViewItem =NewListViewItem (); $listviewitem.subitems[0]. Text =item. Name; - LISTVIEWITEM.SUBITEMS.ADD (item. SEX); - Mycache.add (ListViewItem); the             } -Listview1.virtuallistsize =Mycache.count;Wuyi         } the  -         PrivateList<student>getstudentlist () Wu         { -list<student> list =NewList<student>(); About              for(inti =0; I < -; i++) $             { -Student stu =NewStudent {Name ="Student"+ I, Sex ="male" }; - list. ADD (Stu); -             } A             returnlist; +         } the  -  $         Private voidButton2_Click (Objectsender, EventArgs e) the         { the   theListViewItem ListItem =NewListViewItem (); thelistitem.subitems[0]. Text ="female"; -LISTITEM.SUBITEMS.ADD ("haha"); in Mycache.add (listItem); theListview1.virtuallistsize =Mycache.count; the listview1.invalidate (); About         } the  the     } the  +      Public classStudent -     { the          Public stringSex {Get;Set; }Bayi          Public stringName {Get;Set; } the     } the}

  Summarize

(1) must set VirtualMode to True and set virtuallistsize size

(2) Bind the event Retrievevirtualitem

  (3) If the data in the middle update needs to be reset virtuallistsize, and call the Invalidate () method

(4) Disable SelectedItem, use SelectedItem in this mode will produce an exception, you can use the following method instead

Private List<listviewitem> Findselectedall () {    Listnew list<listviewitem>();     foreach (int in listview1.selectedindices)    {         r.add (Bufferitems[item]);     }      return

WinForm ListView Virtual Mode load data to increase loading speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.