Enhancing the functionality of ListView controls in C #

Source: Internet
Author: User

The first is to implement the custom sort of the ListView control, subscribing to the Columnclick event for the ListView control

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
 if (this.listView1.Columns[e.Column].Tag == null)
  this.listView1.Columns[e.Column].Tag = true;
  bool tabK = (bool)this.listView1.Columns[e.Column].Tag;
 if (tabK)
  this.listView1.Columns[e.Column].Tag = false;
 else
  this.listView1.Columns[e.Column].Tag = true;
  this.listView1.ListViewItemSorter = new ListViewSort(e.Column, this.listView1.Columns[e.Column].Tag); 
  //指定排序器并传送列索引与升序降序关键字
  this.listView1.Sort();//对列表进行自定义排序
}

Definition of Sort class:

///
///自定义ListView控件排序函数
///
class ListViewSort : IComparer
{
 private int col;
 private bool descK;
 public ListViewSort()
 {
  col = 0;
 }
 public ListViewSort(int column, object Desc)
 {
  descK = (bool)Desc;
  col = column; //当前列,0,1,2...,参数由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;
 }
}

The custom arrangement of the above ListView control, which is sorted when you click the caption of the ListView control

The following implements the removal of the last column of the ListView control, which automatically adjusts the appropriate size

First write a function that adjusts the width of the ListView control column

///
///自动调整listView控件最后一列的列宽
///
///
///
private void 调整LV列宽()
{
 listView1.ColumnWidthChanged -= new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
 备注.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
 listView1.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.