listview winfrom 表头_winform ListView点击行表头,排序
第一種辦法:
將ListView的數據放入DataView中,設置DataView的Sort屬性,然后將DataView中的內容在ListView中展示出來。
(1)初始化ListView,將ListView的數據存入DataTable中
(2)從DataTable中獲得DataView
(3)設置DataView的Sort屬性,根據條件進行設定
(4)將DataView中的數據在ListView中展示出來
aaa.txt:
1 usingSystem;2 usingSystem.Data;3 usingSystem.IO;4 usingSystem.Text;5 usingSystem.Windows.Forms;6
7 namespacebatch8 {9 public partial classForm3 : Form10 {11 publicForm3()12 {13 InitializeComponent();14 }15 DataTable dt = null;16 private void Form3_Load(objectsender, EventArgs e)17 {18 //添加DataTable的列表頭
19 dt = newDataTable();20 dt.Columns.Add("num");21 dt.Columns.Add("name");22 dt.Columns.Add("age");23 string path = @"C:\Users\Administrator\Desktop\aaa.txt";24 //讀取文本文件內容
25 string[] ss =File.ReadAllLines(path, Encoding.Default);26 foreach (string s inss)27 {28 string[] a = s.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);29 //往listView1中添加數據
30 ListViewItem li = newListViewItem();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 ///
40 ///listView1的列頭點擊事件41 ///
42 ///
43 ///
44 private void listView1_ColumnClick(objectsender, ColumnClickEventArgs e)45 {46 if (dt == null) return;47 //獲得有內容的DataView
48 DataView dv = newDataView(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 indv)65 {66 ListViewItem li = newListViewItem();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 }
View Code
重寫一個排序方法,進行方法綁定,可以具體問題具體分析,靈活性大。
1 classListViewColumnSorter : IComparer2 {3 private intcolumnToSort;4 privateSortOrder orderOfSort;5 privateCaseInsensitiveComparer objectCompare;6 publicListViewColumnSorter()7 {8 columnToSort = 0;9 orderOfSort =SortOrder.None;10 objectCompare = newCaseInsensitiveComparer();11 }12
13
14 int IComparer.Compare(object x, objecty)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, outyy))23 result =xx.CompareTo(yy);24 else
25 result =xtext.CompareTo(ytext);26 if (orderOfSort ==SortOrder.Ascending)27 returnresult;28 else if (orderOfSort ==SortOrder.Descending)29 return -result;30 else
31 return 0;32 }33 public intsortColumn34 {35 set { columnToSort =value; }36 get { returncolumnToSort; }37 }38 publicSortOrder order39 {40 set { orderOfSort =value; }41 get { returnorderOfSort; }42 }43
44
45
46 }47
48 classListViewHelper49 {50 publicListViewHelper() { }51 public static void listView_columnClick(objectsender, ColumnClickEventArgs e)52 {53
54 ListView lv = sender asListView;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 }
View Code
以上代碼封裝后無須更改,在窗體中添加一個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);
}
完!
總結
以上是生活随笔為你收集整理的listview winfrom 表头_winform ListView点击行表头,排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rust军用船指令_RUST物品指令清单
- 下一篇: python 分布式锁_python分布