最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C# listview 点击列头排序的实例
时间:2022-06-25 07:55:45 编辑:袖梨 来源:一聚教程网
代码如下 | 复制代码 |
#region 自定义变量 intcurrentCol = -1; boolsort; #endregion//列头点击事件
privatevoidlvw_ColumnClick(objectsender, ColumnClickEventArgs e) { stringAsc = ((char)0x25bc).ToString().PadLeft(4,' '); stringDes = ((char)0x25b2).ToString().PadLeft(4,' ');
if(sort ==false) { sort =true; stringoldStr =this.lvw.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2,' '); this.lvw.Columns[e.Column].Text = oldStr + Des; } elseif(sort ==true) { sort =false; stringoldStr =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")//在设计器中把列头的tag设为"n",则表示该列按数字比较器处理,否则为文本 lvw.ListViewItemSorter =newListViewItemComparerNum(e.Column, sort); else lvw.ListViewItemSorter =newListViewItemComparer(e.Column, sort); this.lvw.Sort(); introwCount =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 { publicboolsort_b; publicSortOrder order = SortOrder.Ascending;
privateintcol;
publicListViewItemComparer() { col = 0; }
publicListViewItemComparer(intcolumn,boolsort) { col = column; sort_b = sort; }
publicintCompare(objectx,objecty) { if(sort_b) { returnString.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text); } else { returnString.Compare(((ListViewItem)y).SubItems[col].Text, ((ListViewItem)x).SubItems[col].Text); } } } //数字比较器 publicclassListViewItemComparerNum : IComparer { publicboolsort_b; publicSortOrder order = SortOrder.Ascending;
privateintcol;
publicListViewItemComparerNum() { col = 0; }
publicListViewItemComparerNum(intcolumn,boolsort) { col = column; sort_b = sort; }
publicintCompare(objectx,objecty) { decimald1=Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text); decimald2=Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text); if(sort_b) { returndecimal.Compare(d1,d2); } else { returndecimal.Compare(d2,d1); } } } |