SoFunction
Updated on 2025-03-08

C# listview Click on the column header to sort instance

C# listview Click on the column header to sort instance

Updated: January 25, 2017 08:48:00 Submission: jingxian
Below, the editor will bring you an example of clicking on the column header sorting in C# listview. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor

Examples are as follows:

#region Custom variables  int currentCol = -1;
  bool sort;
  #endregion//Column header click event
private void lvw_ColumnClick(object sender, ColumnClickEventArgs e)
  {
   string Asc = ((char)0x25bc).ToString().PadLeft(4, ' ');
   string Des = ((char)0x25b2).ToString().PadLeft(4, ' ');

   if (sort == false)
   {
    sort = true;
    string oldStr = [].((char)0x25bc, (char)0x25b2, ' ');
    [].Text = oldStr + Des;
   }
   else if (sort == true)
   {
    sort = false;
    string oldStr = [].((char)0x25bc, (char)0x25b2, ' ');
    [].Text = oldStr + Asc;
   }

   if([].()=="n")//Set the tag of the column header to "n" in the designer, which means that the column is processed as a digital comparator, otherwise it is text     = new ListViewItemComparerNum(, sort);
   else
     = new ListViewItemComparer(, sort);
   ();
   int rowCount = ;
   if (currentCol != -1)
   {
    if ( != currentCol)
     [currentCol].Text = [currentCol].((char)0x25bc, (char)0x25b2, ' ');
   }
   currentCol = ;
  }//Text comparator public class ListViewItemComparer: IComparer  {
   public bool sort_b;
   public SortOrder order = ;

   private int col;

   public ListViewItemComparer()
   {
    col = 0;
   }

   public ListViewItemComparer(int column, bool sort)
   {
    col = column;
    sort_b = sort;
   }

   public int Compare(object x, object y)
   {
    if (sort_b)
    {
     return (((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
    }
    else
    {
     return (((ListViewItem)y).SubItems[col].Text, ((ListViewItem)x).SubItems[col].Text);
    }
   }
  }  //Digital Comparator  public class ListViewItemComparerNum : IComparer
  {
   public bool sort_b;
   public SortOrder order = ;

   private int col;

   public ListViewItemComparerNum()
   {
    col = 0;
   }

   public ListViewItemComparerNum(int column, bool sort)
   {
    col = column;
    sort_b = sort;
   }

   public int Compare(object x, object y)
   {
    decimal d1=(((ListViewItem)x).SubItems[col].Text);
    decimal d2=(((ListViewItem)y).SubItems[col].Text);
    if (sort_b)
    {
     return (d1,d2);
    }
    else
    {
     return (d2,d1);
    }
   }
  }

The above example of clicking the column header sorting of C# listview is all the content I share with you. I hope you can give you a reference and I hope you can support me more.

  • c
  • listview
  • Click
  • Sort

Related Articles

  • A reversible encrypted class (using 3DES encryption)

    Represents the base class of the triple data encryption standard algorithm, all implementations of TripleDES must be derived from this base class. It is inherited from the SymmetricAlgorithm class.
    2011-07-07
  • Detailed explanation of 24-point game examples implemented by C#

    This article mainly introduces the 24-point game implemented in C#, and analyzes the algorithm principles and implementation techniques of the 24-point game in detail. It has certain reference value. Friends who need it can refer to it.
    2015-08-08
  • richtextbox control insert link code sharing

    The richtextbox control inserts the link, and temporarily uses this to solve the Chinese text display of the link
    2013-12-12
  • C# implements the method of adding watermark to images

    This article mainly introduces the method of adding watermarks to images in C#. It analyzes the common image watermark operation related implementation techniques in C# with the complete example form. Friends who need it can refer to it.
    2016-02-02
  • In-depth analysis of the Unity editor resource import processing function OnPostprocessTexture instance

    This article mainly introduces the In-depth analysis of the OnPostprocessTexture instance of the Unity editor resource import processing function. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase as soon as possible.
    2023-09-09
  • Implementation of C# dll code obfuscated encryption

    This article mainly introduces the implementation of C# dll code obfuscation encryption. The example code is introduced in this article in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2023-02-02
  • C#.NET uses HTML template to send complete examples of emails

    This article mainly introduces the method of C#.NET using HTML templates to send emails, mainly including three parts: HTML template, replacement function and email function. It is a very practical function. Friends who need it can refer to it.
    2014-09-09
  • Simple linked list class instance implemented by C#

    This article mainly introduces the simple linked list class implemented by C#, involving C#’s definition, implementation, and the addition, deletion and modification techniques of linked list nodes. It has certain reference value. Friends who need it can refer to it.
    2015-08-08
  • c# Example of using wmi to query USB device information

    This article mainly introduces the example of using WMI to query the Usb device information in C#. Please refer to it.
    2014-01-01
  • The role of c# hidden base class method

    This article mainly introduces the role of the c# hidden base class method, you can refer to it and use it
    2013-12-12

Latest Comments