Freigeben über


How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control

In the DataGridView control, text box columns use automatic sorting by default, while other column types are not sorted automatically. Sometimes you will want to override these defaults. For example, you can display images in place of text, numbers, or enumeration cell values. While the images cannot be sorted, the underlying values that they represent can be sorted.

In the DataGridView control, the SortMode property value of a column determines its sorting behavior.

The following procedure shows the Priority column from How to: Customize Data Formatting in the Windows Forms DataGridView Control. This column is an image column and is not sortable by default. It contains actual cell values that are strings, however, so it can be sorted automatically.

To set the sort mode for a column

  • Set the System.Windows.Forms.DataGridViewColumn.SortMode property.

    Me.dataGridView1.Columns("Priority").SortMode = _
        DataGridViewColumnSortMode.Automatic
    
    this.dataGridView1.Columns["Priority"].SortMode =
        DataGridViewColumnSortMode.Automatic;
    

Compiling the Code

This example requires:

  • A DataGridView control named dataGridView1 that contains a column named Priority.

  • References to the System and System.Windows.Forms assemblies.

See Also

Reference

DataGridView
System.Windows.Forms.DataGridViewColumn.SortMode

Concepts

Column Sort Modes in the Windows Forms DataGridView Control
How to: Customize Sorting in the Windows Forms DataGridView Control

Other Resources

Sorting Data in the Windows Forms DataGridView Control