C# ListView 點(diǎn)擊表頭對數(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();//對列表進(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;
}
}
}
注意:
有的會報(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#使用Task實(shí)現(xiàn)執(zhí)行并行任務(wù)的原理的示例詳解
Task是一個(gè)表示異步操作的類,它提供了一種簡單、輕量級的方式來創(chuàng)建多線程應(yīng)用程序。本文就來和大家聊聊在C#中如何使用Task執(zhí)行并行任務(wù)吧2023-04-04
C#畫筆Pen保存和恢復(fù)圖形對象的設(shè)置方法
這篇文章主要介紹了C#畫筆Pen保存和恢復(fù)圖形對象的設(shè)置方法,實(shí)例分析了畫筆的保存save及恢復(fù)屬性Restore的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06
c# 從內(nèi)存中釋放Selenium chromedriver.exe
這篇文章主要介紹了c# 從內(nèi)存中釋放Selenium chromedriver.exe的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)在服務(wù)器端裁剪圖片的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在服務(wù)器端裁剪圖片的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-04-04
c# 數(shù)據(jù)類型占用的字節(jié)數(shù)介紹
本篇文章主要是對c#中數(shù)據(jù)類型占用的字節(jié)數(shù)進(jìn)行了詳細(xì)的介紹。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01

