ListView排序并隔色显示
生活随笔
收集整理的這篇文章主要介紹了
ListView排序并隔色显示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;namespace STools
{class ListViewColumnSorter : IComparer{/**//// <summary>/// 指定按照哪個列排序/// </summary>private int ColumnToSort;/**//// <summary>/// 指定排序的方式/// </summary>private SortOrder OrderOfSort;/**//// <summary>/// 聲明CaseInsensitiveComparer類對象,/// 參見ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemCollectionsCaseInsensitiveComparerClassTopic.htm/// </summary>private CaseInsensitiveComparer ObjectCompare; /**//// <summary>/// 構造函數/// </summary>public ListViewColumnSorter(){// 默認按第一列排序ColumnToSort = 0; // 排序方式為不排序OrderOfSort = SortOrder.None; // 初始化CaseInsensitiveComparer類對象ObjectCompare = new CaseInsensitiveComparer();} /**//// <summary>/// 重寫IComparer接口./// </summary>/// <param name="x">要比較的第一個對象</param>/// <param name="y">要比較的第二個對象</param>/// <returns>比較的結果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>public int Compare(object x, object y){int compareResult;ListViewItem listviewX, listviewY; // 將比較對象轉換為ListViewItem對象listviewX = (ListViewItem)x;listviewY = (ListViewItem)y; // 比較compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);//Console.WriteLine(listviewX.SubItems[ColumnToSort].Text + "<->" + listviewY.SubItems[ColumnToSort].Text+":"+compareResult);// 根據上面的比較結果返回正確的比較結果if (OrderOfSort == SortOrder.Ascending){// 因為是正序排序,所以直接返回結果return compareResult;}else if (OrderOfSort == SortOrder.Descending){// 如果是反序排序,所以要取負值再返回return (-compareResult);}else{// 如果相等返回0return 0;}} /**//// <summary>/// 獲取或設置按照哪一列排序./// </summary>public int SortColumn{set{ColumnToSort = value;}get{return ColumnToSort;}} /**//// <summary>/// 獲取或設置排序方式./// </summary>public SortOrder Order{set{OrderOfSort = value;}get{return OrderOfSort;}}}
}
下面是應用排序,并不同值隔色顯示
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e){ListView lv = sender as ListView;// 檢查點擊的列是不是現在的排序列. int column = (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn;//當前排序的序列listView1.Columns[column].Text = listView1.Columns[column].Text.Replace("↓", "").Replace("↑", "");if (e.Column == (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn){// 重新設置此列的排序方法. if ((lv.ListViewItemSorter as ListViewColumnSorter).Order == SortOrder.Ascending){(lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Descending;listView1.Columns[e.Column].Text += "↑";}else{(lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;listView1.Columns[e.Column].Text += "↓";}}else{// 設置排序列,默認為正向排序 (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;(lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;listView1.Columns[e.Column].Text += "↓";}// 用新的排序方法對ListView排序 ((ListView)sender).Sort();for (int i = 0; i < listView1.Items.Count; i++){if (i != 0 && listView1.Items[i].SubItems[e.Column].Text == listView1.Items[i - 1].SubItems[e.Column].Text){listView1.Items[i].BackColor = listView1.Items[i - 1].BackColor;}else if (i == 0){listView1.Items[i].BackColor = Color.Transparent;}else{if (listView1.Items[i - 1].BackColor == Color.Transparent){listView1.Items[i].BackColor = Color.LightBlue;}else{listView1.Items[i].BackColor = Color.Transparent;}}}//listView1.Refresh();}?
?
轉載于:https://www.cnblogs.com/haorui/p/3608841.html
總結
以上是生活随笔為你收集整理的ListView排序并隔色显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FireMonkey 保存图片到JPG的
- 下一篇: 小谈@override