Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

DataGridView.SortedColumn Property

Gets the column by which the DataGridView contents are currently sorted.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
<BrowsableAttribute(False)> _
Public ReadOnly Property SortedColumn As DataGridViewColumn

Property Value

Type: System.Windows.Forms.DataGridViewColumn
The DataGridViewColumn by which the DataGridView contents are currently sorted.

If the DataGridView is not sorted, this property will return Nothing.

When the column indicated by this property has a SortMode property value of DataGridViewColumnSortMode.Automatic, it will display a sorting glyph based on the value of the SortOrder property.

When the column has a SortMode property value of DataGridViewColumnSortMode.Programmatic, you must display the sorting glyph yourself through the DataGridViewColumnHeaderCell.SortGlyphDirection property.

NoteNote

The value of this property is not meaningful when you sort the control using custom sorting. For more information about custom sorting, see the Sort(IComparer) method and the SortCompare event.

The following code example demonstrates how to use the SortedColumn property in a programmatic sort.

Private Sub SortButton_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles sortButton.Click

    ' Check which column is selected, otherwise set NewColumn to Nothing. 
    Dim newColumn As DataGridViewColumn
    If dataGridView1.Columns.GetColumnCount(DataGridViewElementStates _
        .Selected) = 1 Then
        newColumn = dataGridView1.SelectedColumns(0)
    Else
        newColumn = Nothing 
    End If 

    Dim oldColumn As DataGridViewColumn = dataGridView1.SortedColumn
    Dim direction As ListSortDirection

    ' If oldColumn is null, then the DataGridView is not currently sorted. 
    If oldColumn IsNot Nothing Then 

        ' Sort the same column again, reversing the SortOrder. 
        If oldColumn Is newColumn AndAlso dataGridView1.SortOrder = _
            SortOrder.Ascending Then
            direction = ListSortDirection.Descending
        Else 

            ' Sort a new column and remove the old SortGlyph.
            direction = ListSortDirection.Ascending
            oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
        End If 
    Else
        direction = ListSortDirection.Ascending
    End If 


    ' If no column has been selected, display an error dialog  box. 
    If newColumn Is Nothing Then
        MessageBox.Show("Select a single column and try again.", _
            "Error: Invalid Selection", MessageBoxButtons.OK, _
            MessageBoxIcon.Error)
    Else
        dataGridView1.Sort(newColumn, direction)
        If direction = ListSortDirection.Ascending Then
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
        Else
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
        End If 
    End If 

End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft