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# winform treelistview的使用(treegridview)實(shí)例詳解
- C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
- C# WPF ListView控件的實(shí)例詳解
- 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#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 一文掌握C# ListView控件的用法和示例代碼
相關(guān)文章
C#模擬實(shí)現(xiàn)鼠標(biāo)自動(dòng)點(diǎn)擊與消息發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了C#如何利用windows api來(lái)模擬實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊、右擊、雙擊以及發(fā)送文本功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08
C# WPF 建立無(wú)邊框(標(biāo)題欄)的登錄窗口的示例
這篇文章主要介紹了C# WPF 建立無(wú)邊框(標(biāo)題欄)的登錄窗口的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12
C#使用HtmlAgilityPack抓取糗事百科內(nèi)容實(shí)例
這篇文章主要介紹了C#使用HtmlAgilityPack抓取糗事百科內(nèi)容的方法,實(shí)例分析了C#中HtmlAgilityPack的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C#實(shí)現(xiàn)最完整的文件和目錄操作類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)最完整的文件和目錄操作類,涉及C#針對(duì)文件與目錄的創(chuàng)建、獲取、檢測(cè)、刪除等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
C#連續(xù)任務(wù)Task.ContinueWith方法
這篇文章介紹了C#中的連續(xù)任務(wù)Task.ContinueWith方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

