第一种办法:
将ListView的数据放入DataView中,设置DataView的Sort属性,然后将DataView中的内容在ListView中展示出来。
(1)初始化ListView,将ListView的数据存入DataTable中
(2)从DataTable中获得DataView
(3)设置DataView的Sort属性,根据条件进行设定
(4)将DataView中的数据在ListView中展示出来
aaa.txt:
1 using System; 2 using System.Data; 3 using System.IO; 4 using System.Text; 5 using System.Windows.Forms; 6 7 namespace batch 8 { 9 public partial class Form3 : Form 10 { 11 public Form3() 12 { 13 InitializeComponent(); 14 } 15 DataTable dt = null; 16 private void Form3_Load(object sender, EventArgs e) 17 { 18 //添加DataTable的列表头 19 dt = new DataTable(); 20 dt.Columns.Add("num"); 21 dt.Columns.Add("name"); 22 dt.Columns.Add("age"); 23 string path = @"C:UsersAdministratorDesktopaaa.txt"; 24 //读取文本文件内容 25 string[] ss = File.ReadAllLines(path, Encoding.Default); 26 foreach (string s in ss) 27 { 28 string[] a = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 29 //往listView1中添加数据 30 ListViewItem li = new ListViewItem(); 31 li.Text = a[0]; 32 li.SubItems.AddRange(new string[] { a[1], a[2] }); 33 listView1.Items.Add(li); 34 //往DataTable中添加行数据 35 dt.Rows.Add(a[0], a[1], a[2]); 36 } 37 } 38 int sortfield = -1; 39 /// <summary> 40 /// listView1的列头点击事件 41 /// </summary> 42 /// <param name="sender"></param> 43 /// <param name="e"></param> 44 private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) 45 { 46 if (dt == null) return; 47 //获得有内容的DataView 48 DataView dv = new DataView(dt); 49 //获得所点列的索引 50 int sf = e.Column; 51 if (sortfield == sf) 52 { 53 //设置sort 54 dv.Sort = dt.Columns[sf].ColumnName + " desc"; 55 sortfield = -1; 56 } 57 else 58 { 59 dv.Sort = dt.Columns[sf].ColumnName + " asc"; 60 sortfield = sf; 61 } 62 //展示DataView中的内容 63 listView1.Items.Clear(); 64 foreach (DataRowView drv in dv) 65 { 66 ListViewItem li = new ListViewItem(); 67 li.Text = drv[0].ToString(); 68 li.SubItems.Add(drv[1].ToString()); 69 li.SubItems.Add(drv[2].ToString()); 70 listView1.Items.Add(li); 71 } 72 73 } 74 } 75 }
第二种方法:转自http://bbs.csdn.net/topics/350071962
重写一个排序方法,进行方法绑定,可以具体问题具体分析,灵活性大。
1 class ListViewColumnSorter : IComparer 2 { 3 private int columnToSort; 4 private SortOrder orderOfSort; 5 private CaseInsensitiveComparer objectCompare; 6 public ListViewColumnSorter() 7 { 8 columnToSort = 0; 9 orderOfSort = SortOrder.None; 10 objectCompare = new CaseInsensitiveComparer(); 11 } 12 13 14 int IComparer.Compare(object x, object y) 15 { 16 int result = 0; 17 ListViewItem lx = (ListViewItem)x; 18 ListViewItem ly = (ListViewItem)y; 19 string xtext = lx.SubItems[columnToSort].Text; 20 string ytext = ly.SubItems[columnToSort].Text; 21 int xx = 0, yy = 0; 22 if (int.TryParse(xtext, out xx) && int.TryParse(ytext, out yy)) 23 result = xx.CompareTo(yy); 24 else 25 result = xtext.CompareTo(ytext); 26 if (orderOfSort == SortOrder.Ascending) 27 return result; 28 else if (orderOfSort == SortOrder.Descending) 29 return -result; 30 else 31 return 0; 32 } 33 public int sortColumn 34 { 35 set { columnToSort = value; } 36 get { return columnToSort; } 37 } 38 public SortOrder order 39 { 40 set { orderOfSort = value; } 41 get { return orderOfSort; } 42 } 43 44 45 46 } 47 48 class ListViewHelper 49 { 50 public ListViewHelper() { } 51 public static void listView_columnClick(object sender, ColumnClickEventArgs e) 52 { 53 54 ListView lv = sender as ListView; 55 lv.Cursor = Cursors.Hand; 56 if (e.Column == ((ListViewColumnSorter)lv.ListViewItemSorter).sortColumn)//排序方式 57 { 58 if (((ListViewColumnSorter)lv.ListViewItemSorter).order == SortOrder.Ascending) 59 ((ListViewColumnSorter)lv.ListViewItemSorter).order = SortOrder.Descending; 60 else 61 ((ListViewColumnSorter)lv.ListViewItemSorter).order = SortOrder.Ascending; 62 63 } 64 else 65 { 66 ((ListViewColumnSorter)lv.ListViewItemSorter).sortColumn = e.Column; 67 ((ListViewColumnSorter)lv.ListViewItemSorter).order = SortOrder.Ascending; 68 } 69 lv.Sort(); 70 } 71 }
以上代码封装后无须更改,在窗体中添加一个ListView控件,在窗体的Load事件中添加如下代码:
private void Form1_Load(object sender, EventArgs e) { this.listView1.ListViewItemSorter = new Common.ListViewColumnSorter(); this.listView1.ColumnClick += new ColumnClickEventHandler(Common.ListViewHelper.ListView_ColumnClick); }
完!