DataGridViewColumnHeaderCell.SortGlyphDirection Eigenschaft

Definition

Ruft einen Wert ab, der die Art des angezeigten Sortiersymbols angibt, oder legt diesen fest.

public:
 property System::Windows::Forms::SortOrder SortGlyphDirection { System::Windows::Forms::SortOrder get(); void set(System::Windows::Forms::SortOrder value); };
public System.Windows.Forms.SortOrder SortGlyphDirection { get; set; }
member this.SortGlyphDirection : System.Windows.Forms.SortOrder with get, set
Public Property SortGlyphDirection As SortOrder

Eigenschaftswert

Ein SortOrder-Wert, der das aktuelle Symbol darstellt. Der Standardwert ist None.

Ausnahmen

Der beim Festlegen dieser Eigenschaft angegebene Wert ist kein gültiger SortOrder-Wert.

Beim Festlegen dieser Eigenschaft ist entweder der Wert der OwningColumn-Eigenschaft oder der Wert der DataGridView-Eigenschaft der Zelle null.

- oder -

Beim Ändern dieser Eigenschaft ist der angegebene Wert nicht None, und der Wert der SortMode-Eigenschaft der besitzenden Spalte ist NotSortable.

Beispiele

Der folgende Code veranschaulicht, wie die SortGlyphDirection -Eigenschaft in einer programmgesteuerten Sortierung verwendet wird.

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;
    }
}
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

Hinweise

Diese Eigenschaft wird automatisch für Spalten festgelegt, deren SortMode Eigenschaft auf DataGridViewColumnSortMode.Automaticfestgelegt ist. Sie müssen diese Eigenschaft selbst festlegen, wenn Sie nach Spalten sortieren, wobei die SortMode -Eigenschaft auf DataGridViewColumnSortMode.Programmaticfestgelegt ist.

Gilt für:

Weitere Informationen