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 DataGridViewColumn.SortMode property.

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

Compiling the Code

This example requires:

See also