DataGridView.SortedColumn DataGridView.SortedColumn DataGridView.SortedColumn DataGridView.SortedColumn Property

Definition

Ruft die Spalte ab, nach der der DataGridView-Inhalt gerade sortiert wird.Gets the column by which the DataGridView contents are currently sorted.

public:
 property System::Windows::Forms::DataGridViewColumn ^ SortedColumn { System::Windows::Forms::DataGridViewColumn ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewColumn SortedColumn { get; }
member this.SortedColumn : System.Windows.Forms.DataGridViewColumn
Public ReadOnly Property SortedColumn As DataGridViewColumn

Eigenschaftswert

Die DataGridViewColumn, nach der der DataGridView-Inhalt gerade sortiert wird.The DataGridViewColumn by which the DataGridView contents are currently sorted.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die SortedColumn -Eigenschaft in einer programmgesteuerten Sortierung verwendet wird.The following code example demonstrates how to use the SortedColumn 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;
    }
}
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

Wenn die DataGridView nicht sortiert ist, gibt diese Eigenschaft zurück null.If the DataGridView is not sorted, this property will return null.

Wenn die Spalte DataGridViewColumnSortMode.Automatic, die durch diese Eigenschaft angegeben SortMode ist, den-Eigenschafts Wert hat, wird ein Sortier Symbol basierend SortOrder auf dem Wert der-Eigenschaft angezeigt.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.

Wenn die-Spalte einen SortMode -Eigenschafts DataGridViewColumnSortMode.ProgrammaticWert von aufweist, müssen Sie das Sortier Symbol selbst über DataGridViewColumnHeaderCell.SortGlyphDirection die-Eigenschaft anzeigen.When the column has a SortMode property value of DataGridViewColumnSortMode.Programmatic, you must display the sorting glyph yourself through the DataGridViewColumnHeaderCell.SortGlyphDirection property.

Hinweis

Der Wert dieser Eigenschaft ist nicht sinnvoll, wenn Sie das Steuerelement mithilfe der benutzerdefinierten Sortierung sortieren.The value of this property is not meaningful when you sort the control using custom sorting. Weitere Informationen zur benutzerdefinierten Sortierung finden Sie unter Sort(IComparer) der-Methode SortCompare und dem-Ereignis.For more information about custom sorting, see the Sort(IComparer) method and the SortCompare event.

Gilt für:

Siehe auch