C# listview 點(diǎn)擊列頭排序的實(shí)例
更新時(shí)間:2017年01月25日 08:48:00 投稿:jingxian
下面小編就為大家?guī)?lái)一篇C# listview 點(diǎn)擊列頭排序的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
實(shí)例如下:
#region 自定義變量 int currentCol = -1; bool sort; #endregion//列頭點(diǎn)擊事件 private void lvw_ColumnClick(object sender, ColumnClickEventArgs e) { string Asc = ((char)0x25bc).ToString().PadLeft(4, ' '); string Des = ((char)0x25b2).ToString().PadLeft(4, ' '); if (sort == false) { sort = true; string oldStr = this.lvw.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' '); this.lvw.Columns[e.Column].Text = oldStr + Des; } else if (sort == true) { sort = false; string oldStr = this.lvw.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' '); this.lvw.Columns[e.Column].Text = oldStr + Asc; } if(lvw.Columns[e.Column].Tag.ToString()=="n")//在設(shè)計(jì)器中把列頭的tag設(shè)為"n",則表示該列按數(shù)字比較器處理,否則為文本 lvw.ListViewItemSorter = new ListViewItemComparerNum(e.Column, sort); else lvw.ListViewItemSorter = new ListViewItemComparer(e.Column, sort); this.lvw.Sort(); int rowCount = this.lvw.Items.Count; if (currentCol != -1) { if (e.Column != currentCol) this.lvw.Columns[currentCol].Text = this.lvw.Columns[currentCol].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' '); } currentCol = e.Column; }//文本比較器public class ListViewItemComparer : IComparer { public bool sort_b; public SortOrder order = SortOrder.Ascending; private int col; public ListViewItemComparer() { col = 0; } public ListViewItemComparer(int column, bool sort) { col = column; sort_b = sort; } public int Compare(object x, object y) { if (sort_b) { return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text); } else { return String.Compare(((ListViewItem)y).SubItems[col].Text, ((ListViewItem)x).SubItems[col].Text); } } } //數(shù)字比較器 public class ListViewItemComparerNum : IComparer { public bool sort_b; public SortOrder order = SortOrder.Ascending; private int col; public ListViewItemComparerNum() { col = 0; } public ListViewItemComparerNum(int column, bool sort) { col = column; sort_b = sort; } public int Compare(object x, object y) { decimal d1=Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text); decimal d2=Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text); if (sort_b) { return decimal.Compare(d1,d2); } else { return decimal.Compare(d2,d1); } } }
以上這篇C# listview 點(diǎn)擊列頭排序的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
- C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼
- C#中ListView控件實(shí)現(xiàn)窗體代碼
- C#下listview如何插入圖片
- C#實(shí)現(xiàn)listview Group收縮擴(kuò)展的方法
- C#實(shí)現(xiàn)帶進(jìn)度條的ListView
- C#使用listView增刪操作實(shí)例
- 淺談C#中ListView類的用法
相關(guān)文章

Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析
這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
2023-09-09 
C#.NET采用HTML模板發(fā)送電子郵件完整實(shí)例
這篇文章主要介紹了C#.NET采用HTML模板發(fā)送電子郵件的方法,主要包括了HTML模板、替換函數(shù)與郵件函數(shù)三部分,是非常實(shí)用的功能,需要的朋友可以參考下
2014-09-09 
C#實(shí)現(xiàn)的簡(jiǎn)單鏈表類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的簡(jiǎn)單鏈表類,涉及C#針對(duì)鏈表的定義、實(shí)現(xiàn)及鏈表節(jié)點(diǎn)的增加、刪除與修改技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
2015-08-08