LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

[点晴永久免费OA]C#中listview控件点击列表头实现自动排序

admin
2022年12月22日 20:14 本文热度 587
  1. #region 自定义变量
  2. int currentCol = -1;
  3. bool sort;
  4. #endregion
//列头点击事件
  1. private void lvw_ColumnClick(object sender, ColumnClickEventArgs e)
  2. {
  3. string Asc = ((char)0x25bc).ToString().PadLeft(4, ' ');
  4. string Des = ((char)0x25b2).ToString().PadLeft(4, ' ');
  5. if (sort == false)
  6. {
  7. sort = true;
  8. string oldStr = this.lvw.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
  9. this.lvw.Columns[e.Column].Text = oldStr + Des;
  10. }
  11. else if (sort == true)
  12. {
  13. sort = false;
  14. string oldStr = this.lvw.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
  15. this.lvw.Columns[e.Column].Text = oldStr + Asc;
  16. }
  17. if(lvw.Columns[e.Column].Tag.ToString()=="n")//在设计器中把列头的tag设为"n",则表示该列按数字比较器处理,否则为文本
  18. lvw.ListViewItemSorter = new ListViewItemComparerNum(e.Column, sort);
  19. else
  20. lvw.ListViewItemSorter = new ListViewItemComparer(e.Column, sort);
  21. this.lvw.Sort();
  22. int rowCount = this.lvw.Items.Count;
  23. if (currentCol != -1)
  24. {
  25. if (e.Column != currentCol)
  26. this.lvw.Columns[currentCol].Text = this.lvw.Columns[currentCol].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
  27. }
  28. currentCol = e.Column;
  29. }
//文本比较器
  1. public class ListViewItemComparer : IComparer
  2. {
  3. public bool sort_b;
  4. public SortOrder order = SortOrder.Ascending;
  5. private int col;
  6. public ListViewItemComparer()
  7. {
  8. col = 0;
  9. }
  10. public ListViewItemComparer(int column, bool sort)
  11. {
  12. col = column;
  13. sort_b = sort;
  14. }
  15. public int Compare(object x, object y)
  16. {
  17. if (sort_b)
  18. {
  19. return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
  20. }
  21. else
  22. {
  23. return String.Compare(((ListViewItem)y).SubItems[col].Text, ((ListViewItem)x).SubItems[col].Text);
  24. }
  25. }
  26. }
  1. //数字比较器
  2. public class ListViewItemComparerNum : IComparer
  3. {
  4. public bool sort_b;
  5. public SortOrder order = SortOrder.Ascending;
  6. private int col;
  7. public ListViewItemComparerNum()
  8. {
  9. col = 0;
  10. }
  11. public ListViewItemComparerNum(int column, bool sort)
  12. {
  13. col = column;
  14. sort_b = sort;
  15. }
  16. public int Compare(object x, object y)
  17. {
  18. decimal d1=Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text);
  19. decimal d2=Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text);
  20. if (sort_b)
  21. {
  22. return decimal.Compare(d1,d2);
  23. }
  24. else
  25. {
  26. return decimal.Compare(d2,d1);
  27. }
  28. }
  29. }


该文章在 2022/12/22 20:14:32 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved