DataGridView Use Tip 13: Click on the column header to sort ascending and descending order

Source: Internet
Author: User

The DataGridView column has three sorting modes. The sort mode for each column is specified by the SortMode property of the column, and the property can be set to one of the following Datagridviewcolumnsortmode enumeration values.

Datagridviewcolumnsortmode Value Description:

Automatic
The default sort mode for text box columns. Clicking a column header automatically sorts DataGridView by this column, unless the column header is used for selection, and displays a glyph that indicates the sort order (the triangular arrow upward: sort ascending; downward triangle arrow: descending sort).

Notsortable
The default sort mode for non-text box columns. This column can be sorted programmatically, but this column is not suitable for sorting , so space is not reserved for the sort glyph.

Programmatic
This column can be sorted programmatically, and space is reserved for the sort glyph.

First, use the SortMode property to automatically sort

1, through the program settings

Private void Frmmain_load (object  sender, EventArgs e) {     foreachin this. DGV _users.columns)    {          // Set auto sort          column. SortMode = datagridviewcolumnsortmode.automatic;    }}

2, modify the properties in the design interface

Second, the use of programmatic way to achieve custom sorting

You can programmatically sort DataGridView by the values in either or more columns, regardless of the SortMode setting. sorting Programmatically is useful when you want to provide your own user interface (UI) for sorting , or when you want to implement custom sorting . It is useful to provide your own sort user interface, for example, when setting DataGridView selection mode to enable column header selection. In this case, although the column header cannot be used for sorting , but you still want the header to display the corresponding sort glyph, set the SortMode property to programmatic.

Columns set to programmatic sort mode do not automatically display the sort glyph. For these columns, you must manually display the glyphs by setting the System.Windows.Forms.DataGridViewColumnHeaderCell.SortGlyphDirection property. This is necessary in order to be flexible in custom sorting . For example, if you sort DataGridView by multiple columns, you might want to display more than one sort glyph or no glyph.

For sorted DataGridView, you can determine the sort column and sort ORDER by examining the values of the Sortedcolumn and SortOrder properties.

If the SortMode property is set to programmatic, you need to implement custom sorting in the Columnheadermouseclick event programmatically.

Example:

1 Private voidDgv_users_columnheadermouseclick (Objectsender, DataGridViewCellMouseEventArgs e)2 {          4DataGridView DGV = Sender asDataGridView;5      if(DGV. Columns[e.columnindex]. SortMode = =datagridviewcolumnsortmode.programmatic)6      {7          stringColumnbindingname =DGV. Columns[e.columnindex]. DataPropertyName;8          Switch(DGV. Columns[e.columnindex]. Headercell.sortglyphdirection)9          {Ten              CaseSystem.Windows.Forms.SortOrder.None: One              CaseSystem.Windows.Forms.SortOrder.Ascending: ACustomsort (Columnbindingname,"desc"); -DGV. Columns[e.columnindex]. Headercell.sortglyphdirection =System.Windows.Forms.SortOrder.Descending; -                     Break; the              CaseSystem.Windows.Forms.SortOrder.Descending: -Customsort (Columnbindingname,"ASC"); -DGV. Columns[e.columnindex]. Headercell.sortglyphdirection =System.Windows.Forms.SortOrder.Ascending; -                     Break; +             } -        }           + } A  at /// <summary> - ///Custom Sorting - /// </summary> - /// <param name= "ColumnName" >field name of the binding</param> - /// <param name= "SortMode" >Sort by ASC Ascending desc Descending</param> - Private voidCustomsort (stringColumnbindingname,stringSortMode) in { -DataTable dt = This. Dgv_users.datasource asDataTable; toDataView DV =dt. DefaultView; +Dv. Sort = Columnbindingname +" "+SortMode; -        This. Dgv_users.datasource =DV. ToTable (); the        This. Dgv_users.refresh (); *}

DataGridView Use Tip 13: Click on the column header to sort ascending and descending order

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.