This documentation is archived and is not being maintained.

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)

public SortOrder SortGlyphDirection { get; set; }
/** @property */
public SortOrder get_SortGlyphDirection ()

/** @property */
public void set_SortGlyphDirection (SortOrder value)

public function get SortGlyphDirection () : SortOrder

public function set SortGlyphDirection (value : SortOrder)

Not applicable.

Property Value

A SortOrder value representing the current glyph. The default is None.

Exception typeCondition

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 a null reference (Nothing in Visual Basic).

-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 void sortButton_Click(object sender, System.EventArgs e)
{
    // Check which column is selected, otherwise set NewColumn to null.
    DataGridViewColumn newColumn =
        dataGridView1.Columns.GetColumnCount(
        DataGridViewElementStates.Selected) == 1 ?
        dataGridView1.SelectedColumns[0] : null;

    DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
    ListSortDirection direction;

    // If oldColumn is null, then the DataGridView is not currently sorted.
    if (oldColumn != null)
    {
        // Sort the same column again, reversing the SortOrder.
        if (oldColumn == newColumn &&
            dataGridView1.SortOrder == SortOrder.Ascending)
        {
            direction = ListSortDirection.Descending;
        }
        else
        {
            // Sort a new column and remove the old SortGlyph.
            direction = ListSortDirection.Ascending;
            oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
        }
    }
    else
    {
        direction = ListSortDirection.Ascending;
    }

    // If no column has been selected, display an error dialog  box.
    if (newColumn == null)
    {
        MessageBox.Show("Select a single column and try again.",
            "Error: Invalid Selection", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
    else
    {
        dataGridView1.Sort(newColumn, direction);
        newColumn.HeaderCell.SortGlyphDirection =
            direction == ListSortDirection.Ascending ?
            SortOrder.Ascending : SortOrder.Descending;
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Show: