日期:2014-05-20  浏览次数:21086 次

listView点击列名排序
private int ColumnToSort;// 指定按照哪个列排序 
  private SortOrder OrderOfSort;// 指定排序的方式 
  private CaseInsensitiveComparer ObjectCompare;// 声明CaseInsensitiveComparer类对象
  public void Taxis()// 构造函数
  {
  ColumnToSort = 0;// 默认按第一列排序
  OrderOfSort = SortOrder.None;
  ObjectCompare = new CaseInsensitiveComparer();// 初始化CaseInsensitiveComparer类对象
  }
  public int Compare(object x, object y)// 比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1
  {
  int CompareR;
  ListViewItem listviewX, listviewY;
  listviewX = (ListViewItem)x;
  listviewY = (ListViewItem)y;
  CompareR = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
  if (OrderOfSort == SortOrder.Ascending)
  {
  return CompareR;
  }
  else if (OrderOfSort == SortOrder.Descending)
  {
  return -(CompareR);
  }
  else
  return 0;
  }
  public int SortColumn// 获取或设置按照哪一列排序. 
  {
  set
  {
  ColumnToSort = value;
  return;
  }
  get
  {
  return ColumnToSort;
  }
  }
  public SortOrder Order// 获取或设置排序方式.  
  {
  set
  {
  OrderOfSort = value;
  return;
  }
  get
  {
  return OrderOfSort;
  }
  }
  private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
  {
  if (e.Column == SortColumn)
  {
  if (Order == SortOrder.Ascending)// 重新设置此列的排序方法.
  {
  Order = SortOrder.Descending;
  }
  else
  Order = SortOrder.Ascending;
  }
  else
  {
  SortColumn = e.Column;
  Order = SortOrder.Ascending;
  }
  this.listView1.Sort();
  }



为什么我点击列名什么都没改变啊!?求指导!

------解决方案--------------------
this.listView1.ListViewItemSorter=Compare();