ListView虚拟模式封装

  1  public class ListViewAH : ListViewEx
  2     {
  3         #region 虚拟模式相关操作
  4 
  5         ///<summary>
  6         /// 前台行集合
  7         ///</summary>
  8         public List<ListViewItem> CurrentCacheItemsSource
  9         { get; private set; }
 10 
 11         public ListViewAH()
 12         {
 13             this.CurrentCacheItemsSource = new List<ListViewItem>();
 14             this.VirtualMode = true;
 15             this.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView_RetrieveVirtualItem);
 16         }
 17 
 18         ///<summary>
 19         /// 重置listview集合
 20         ///</summary>
 21         ///<param name="items"></param>
 22         public void ReSet(IList<ListViewItem> items)
 23         {
 24             this.CurrentCacheItemsSource.Clear();
 25             this.CurrentCacheItemsSource = new List<ListViewItem>();
 26             if ( null != items )
 27             {
 28                 if ( items.Count > 0 )
 29                 {
 30                     foreach ( var item in items )
 31                     {
 32                         this.CurrentCacheItemsSource.Add(item);
 33                     }
 34                 }
 35                 this.VirtualListSize = this.CurrentCacheItemsSource.Count;
 36             }
 37             else
 38                 this.VirtualListSize = 0;
 39         }
 40 
 41         /// <summary>
 42         /// 追加项
 43         /// </summary>
 44         /// <param name="l"></param>
 45         public void AppendListViewItem(IList<ListViewItem> l)
 46         {
 47             if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count <= 0 )
 48             {
 49                 ReSet(l);
 50                 return;
 51             }
 52             foreach ( var item in l )
 53             {
 54                 this.CurrentCacheItemsSource.Add(item);
 55             }
 56             this.VirtualListSize = this.CurrentCacheItemsSource.Count;
 57         }
 58 
 59         /// <summary>
 60         /// 清空所有项
 61         /// </summary>
 62         public void ClearAllItem()
 63         {
 64             if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count <= 0 )
 65             {
 66                 return;
 67             }
 68             this.CurrentCacheItemsSource.Clear();
 69             this.VirtualListSize = this.CurrentCacheItemsSource.Count;
 70         }
 71 
 72         /// <summary>
 73         /// 移除当前选中的项
 74         /// </summary>
 75         /// <param name="l"></param>
 76         public void RemoveListViewItem(IList<ListViewItem> l)
 77         {
 78             if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count <= 0 )
 79             {
 80                 return;
 81             }
 82             foreach ( var item in l )
 83             {
 84                 this.CurrentCacheItemsSource.Remove(item);
 85             }
 86             this.VirtualListSize = this.CurrentCacheItemsSource.Count;
 87         }
 88 
 89         ///<summary>
 90         /// 虚拟模式事件
 91         ///</summary>
 92         ///<param name="sender"></param>
 93         ///<param name="e"></param>
 94         private void listView_RetrieveVirtualItem(object sender , RetrieveVirtualItemEventArgs e)
 95         {
 96             if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count == 0 )
 97             {
 98                 return;
 99             }
100 
101             ListViewItem lv = this.CurrentCacheItemsSource[e.ItemIndex];
102             e.Item = lv;
103         }
104 
105         ///<summary>
106         /// 获取选中的第一行的文本值
107         ///</summary>
108         ///<param name="key"></param>
109         ///<returns></returns>
110         public string FirstSelectItemValue(string key)
111         {
112             int i = GetColumnsIndex(key);
113 
114             return this.CurrentCacheItemsSource[this.SelectedIndices[0]].SubItems[i].Text;
115         }
116 
117         ///<summary>
118         /// 获取列名的索引
119         ///</summary>
120         ///<param name="key"></param>
121         ///<returns></returns>
122         public int GetColumnsIndex(string key)
123         {
124             int i = 0;
125             for ( ; i < this.Columns.Count ; i++ )
126             {
127                 if ( this.Columns[i].Name == key )
128                 {
129                     break;
130                 }
131             }
132 
133             return i;
134         }
135 
136         ///<summary>
137         /// 获取选中项
138         ///</summary>
139         ///<returns></returns>
140         public List<ListViewItem> GetSelectItem()
141         {
142             List<ListViewItem> l = new List<ListViewItem>();
143             if ( this.SelectedIndices.Count <= 0 ) return null;
144             foreach ( var item in this.SelectedIndices )
145             {
146                 l.Add(this.CurrentCacheItemsSource[int.Parse(item.ToString())]);
147             }
148             return l;
149         }
150 
151         ///<summary>
152         /// 获取选中行的某列集合
153         ///</summary>
154         ///<param name="key"></param>
155         ///<returns></returns>
156         public List<string> GetListViewField(string key)
157         {
158             List<string> ids = new List<string>();
159 
160             foreach ( var item in this.SelectedIndices )
161             {
162                 string id = this.CurrentCacheItemsSource[int.Parse(item.ToString())].SubItems[GetColumnsIndex(key)].Text;
163                 ids.Add(id);
164             }
165             return ids;
166         }
167 
168         private ListViewItemComparer mySorter;
169         ///<summary>
170         /// 排序
171         ///</summary>
172         ///<param name="e"></param>
173         protected override void OnColumnClick(ColumnClickEventArgs e)
174         {
175             base.OnColumnClick(e);
176 
177             if ( this.mySorter == null )
178             {
179                 this.mySorter = new ListViewItemComparer(e.Column , SortOrder.Ascending);
180             }
181             else
182             {
183                 if ( this.mySorter.SortColumn == e.Column )
184                 {
185                     if ( this.mySorter.Order == SortOrder.Ascending )
186                     {
187                         this.mySorter.Order = SortOrder.Descending;
188                     }
189                     else
190                     {
191                         this.mySorter.Order = SortOrder.Ascending;
192                     }
193                 }
194                 else
195                 {
196                     this.mySorter.SortColumn = e.Column;
197                     this.mySorter.Order = SortOrder.Ascending;
198                 }
199 
200                 this.CurrentCacheItemsSource.Sort(this.mySorter);
201 
202                 this.Invalidate();
203             }
204         }
205         #endregion
206 
207         #region ListView排序逻辑
208         ///<summary>
209         /// ListView排序逻辑
210         ///</summary>
211         private class ListViewItemComparer : System.Collections.Generic.IComparer<ListViewItem>
212         {
213             public ListViewItemComparer()
214             {
215                 this.SortColumn = 0;
216                 this.Order = SortOrder.None;
217             }
218 
219             public ListViewItemComparer(int column)
220                 : this()
221             {
222                 this.SortColumn = column;
223             }
224 
225             ///<summary>
226             /// 
227             ///</summary>
228             ///<param name="column">哪列</param>
229             ///<param name="sortOrder">排序方式</param>
230             public ListViewItemComparer(int column , SortOrder sortOrder)
231                 : this(column)
232             {
233                 Order = sortOrder;
234             }
235 
236             #region IComparer 成员
237             public int Compare(ListViewItem x , ListViewItem y)
238             {
239                 int result = 0;
240                 string c1 = "";
241                 string c2 = "";
242 
243                 try
244                 {
245                     c1 = x.SubItems[this.SortColumn].Text;
246                     c2 = y.SubItems[this.SortColumn].Text;
247                 }
248                 catch ( Exception ex )
249                 {
250                     //    MessageBox.Show(ex.Message);
251                     return 0;
252                 }
253                 result = string.Compare(c1 , c2);
254                 if ( this.Order == SortOrder.Ascending )
255                 {
256                     return result;
257                 }
258                 else if ( this.Order == SortOrder.Descending )
259                 {
260                     return ( -result );
261                 }
262                 else
263                 {
264                     return 0;
265                 }
266             }
267             #endregion
268 
269             ///<summary>
270             /// 当前排序列
271             ///</summary>
272             public int SortColumn
273             {
274                 get;
275                 set;
276             }
277 
278             ///<summary>
279             /// 当前列排序方式
280             ///</summary>
281             public SortOrder Order
282             {
283                 get;
284                 set;
285             }
286         }
287         #endregion
288     }
View Code


使用listView普通模式加载大数据,会比较慢,即时使用线程加载,也会出现不断的刷新,故用虚拟模式下加载,速度快

本例中继承第三方控件的ListViewEx控件,也可以继承系统自带的ListView

原文地址:https://www.cnblogs.com/bbthome/p/3464641.html