1、加载数据、排序、删除、更新、获取选中的值
重新加载llistview时,如果listviewitem的count变化了,必须重新设置 listView1.VirtualListSize,然后 listView1.Invalidate()刷新。
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Windows.Forms; 5 6 namespace WindowsFormsApp32 7 { 8 public partial class Form6 : Form 9 { 10 private List<ListViewItem> m_hListViewItems = null;//设置缓存容器 11 private MMySorter mySort = null;//排序方式 12 public Form6() 13 { 14 InitializeComponent(); 15 m_hListViewItems = new List<ListViewItem>(); 16 mySort = new MMySorter(); 17 #region listview的数据和样式 18 ColumnHeader c1 = new ColumnHeader() { Text = "id" }; 19 ColumnHeader c2 = new ColumnHeader() { Text = "name" }; 20 ColumnHeader c3 = new ColumnHeader() { Text = "bir", Width = 200 }; 21 listView1.Columns.AddRange(new ColumnHeader[] { c1, c2, c3 }); 22 for (int i = 0; i < 20000; i++) 23 { 24 ListViewItem li = new ListViewItem() { Text = i.ToString() }; 25 li.SubItems.Add("name" + i); 26 li.SubItems.Add(DateTime.Now.AddDays(i * 1.00).ToString("yyyy-MM-dd hh:mm:ss")); 27 m_hListViewItems.Add(li); 28 } 29 listView1.GridLines = true; 30 listView1.FullRowSelect = true; 31 listView1.Cursor = Cursors.Hand; 32 #endregion 33 #region listview设置虚拟模式 34 listView1.VirtualMode = true; 35 listView1.VirtualListSize = m_hListViewItems.Count; 36 listView1.RetrieveVirtualItem += ListView1_RetrieveVirtualItem; 37 #endregion 38 listView1.ColumnClick += ListView1_ColumnClick; 39 listView1.SelectedIndexChanged += ListView1_SelectedIndexChanged; 40 listView1.VirtualItemsSelectionRangeChanged += ListView1_VirtualItemsSelectionRangeChanged; 41 } 42 43 private void ListView1_VirtualItemsSelectionRangeChanged(object sender, ListViewVirtualItemsSelectionRangeChangedEventArgs e) 44 { 45 if (e.IsSelected)//连续选中的数量 46 this.Text = (e.EndIndex -e.StartIndex+1).ToString(); 47 } 48 private void ListView1_SelectedIndexChanged(object sender, EventArgs e) 49 { 50 this.Text = listView1.SelectedIndices.Count.ToString(); 51 } 52 private void ListView1_ColumnClick(object sender, ColumnClickEventArgs e) 53 { 54 if (e.Column == mySort.columnToSort) 55 { 56 mySort.so = mySort.so == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending; 57 } 58 else 59 { 60 mySort.so = SortOrder.Ascending; 61 mySort.columnToSort = e.Column; 62 } 63 m_hListViewItems.Sort(mySort);//指定排序方法 64 listView1.Invalidate(); 65 } 66 67 private void ListView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) 68 { 69 if (m_hListViewItems == null || m_hListViewItems.Count == 0) 70 return; 71 e.Item = m_hListViewItems[e.ItemIndex]; 72 if (e.ItemIndex == m_hListViewItems.Count) 73 m_hListViewItems = null; 74 } 75 private void button1_Click(object sender, EventArgs e) 76 { 77 #region 添加数据 78 List<ListViewItem> ll = new List<ListViewItem>(); 79 for (int i = 20000; i < 30000; i++) 80 { 81 ListViewItem li = new ListViewItem() { Text = i.ToString() }; 82 li.SubItems.Add("name" + i); 83 li.SubItems.Add(DateTime.Now.AddDays(i * 1.00).ToString("yyyy-MM-dd hh:mm:ss")); 84 ll.Add(li); 85 } 86 m_hListViewItems.AddRange(ll); 87 listView1.VirtualListSize = m_hListViewItems.Count; 88 #endregion 89 90 } 91 private void button2_Click(object sender, EventArgs e) 92 { 93 #region 更新数据 94 for (int i = 0; i < m_hListViewItems.Count; i++) 95 { 96 m_hListViewItems[i].SubItems[1].Text = m_hListViewItems[i].SubItems[1].Text + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); 97 } 98 listView1.Invalidate(); 99 #endregion 100 } 101 private void button3_Click(object sender, EventArgs e) 102 { 103 #region 删除数据 104 int deleStart = 0; 105 int deleEnd = 100; 106 m_hListViewItems.RemoveRange(deleStart, deleEnd - deleStart); 107 listView1.VirtualListSize = m_hListViewItems.Count; 108 listView1.Invalidate(); 109 #endregion 110 } 111 112 private void button4_Click(object sender, EventArgs e) 113 { 114 #region 获得所选行的数据值 115 StringBuilder sb = new StringBuilder(); 116 foreach (int i in listView1.SelectedIndices) 117 { 118 sb.AppendLine(listView1.Items[i].SubItems[0].Text); 119 } 120 MessageBox.Show(sb.ToString()); 121 #endregion 122 } 123 } 124 }
class MMySorter : IComparer<ListViewItem>//此处必须是IComparer<ListViewItem>接口,不能是IComparer接口,否则报错。 { public int columnToSort { get; set; } public SortOrder so { get; set; } private CaseInsensitiveComparer cic; public MMySorter() { columnToSort = 0; so = SortOrder.None; cic = new CaseInsensitiveComparer(); } int IComparer<ListViewItem>.Compare(ListViewItem x, ListViewItem y) { int result = 0; ListViewItem lx = x; ListViewItem ly = y; string xtext = lx.SubItems[columnToSort].Text; string ytext = ly.SubItems[columnToSort].Text; int xx = 0, yy = 0; if (int.TryParse(xtext, out xx) && int.TryParse(ytext, out yy)) result = xx.CompareTo(yy); else result = xtext.CompareTo(ytext); if (so == SortOrder.Ascending) return result; else if (so == SortOrder.Descending) return -result; else return result; } }
2.checkBox