C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼
添加表頭單擊事件
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) { if (listView1.Columns[e.Column].Tag == null) { listView1.Columns[e.Column].Tag = true; } bool tabK = (bool)listView1.Columns[e.Column].Tag; if (tabK) { listView1.Columns[e.Column].Tag = false; } else { listView1.Columns[e.Column].Tag = true; } listView1.ListViewItemSorter = new ListViewSort(e.Column, listView1.Columns[e.Column].Tag); //指定排序器并傳送列索引與升序降序關(guān)鍵字 listView1.Sort();//對(duì)列表進(jìn)行自定義排序 }
排序用到的類
public class ListViewSort : IComparer { private int col; private bool descK; public ListViewSort() { col = 0; } public ListViewSort(int column, object Desc) { descK = (bool)Desc; col = column; //當(dāng)前列,0,1,2...,參數(shù)由ListView控件的ColumnClick事件傳遞 } public int Compare(object x, object y) { int tempInt = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text); if (descK) { return -tempInt; } else { return tempInt; } } }
注意:
有的會(huì)報(bào)“錯(cuò)誤 CS0305: 使用泛型 類型“System.Collections.Generic.IComparer<T>”需要 1 個(gè)類型參數(shù)”
這時(shí)只需要using System.Collections.Generic;改為using System.Collections; 即可。
上一篇:C#控制臺(tái)程序使用Log4net日志組件詳解
欄 目:C#教程
下一篇:C# 參數(shù)按照ASCII碼從小到大排序(字典序)
本文標(biāo)題:C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5772.html
您可能感興趣的文章
- 01-10WinForm判斷關(guān)閉事件來源于用戶點(diǎn)擊右上角“關(guān)閉”按鈕的方法
- 01-10C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 01-10C#實(shí)現(xiàn)帶進(jìn)度條的ListView
- 01-10C#實(shí)現(xiàn)listview Group收縮擴(kuò)展的方法
- 01-10當(dāng)用戶退出點(diǎn)擊瀏覽器后退仍可回到原來頁面的解決方案
- 01-10C#下listview如何插入圖片
- 01-10WPF的ListView控件自定義布局用法實(shí)例
- 01-10C#中ListView控件實(shí)現(xiàn)窗體代碼
- 01-10C#實(shí)現(xiàn)win10 uwp 右擊浮出窗在點(diǎn)擊位置
- 01-10C#獲取ListView鼠標(biāo)下的Item實(shí)例


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置