Export (0) Print
Expand All

DataGridViewColumnHeaderCell.SortGlyphDirection Property

Gets or sets a value indicating which sort glyph is displayed.

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

'Declaration
Public Property SortGlyphDirection As SortOrder

Property Value

Type: System.Windows.Forms.SortOrder
A SortOrder value representing the current glyph. The default is None.

ExceptionCondition
InvalidEnumArgumentException

The specified value when setting this property is not a valid SortOrder value.

InvalidOperationException

When setting this property, the value of either the OwningColumn property or the DataGridView property of the cell is Nothing.

-or-

When changing the value of this property, the specified value is not None and the value of the SortMode property of the owning column is NotSortable.

This property is set automatically for columns with the SortMode property set to DataGridViewColumnSortMode.Automatic. You must set this property yourself when sorting by columns with the SortMode property set to DataGridViewColumnSortMode.Programmatic.

The following code demonstrates how to use the SortGlyphDirection 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.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft