DataGridView.SortedColumn 属性

定义

获取 DataGridView 内容的当前排序所依据的列。

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

属性值

DataGridView 内容的当前排序所依据的 DataGridViewColumn

属性

示例

下面的代码示例演示如何 SortedColumn 在编程排序中使用 属性。

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

注解

DataGridView如果未排序,则此属性将返回 null

当此属性指示的列的属性值DataGridViewColumnSortMode.AutomaticSortMode 时,它将根据属性的值SortOrder显示排序字形。

当列的属性值DataGridViewColumnSortMode.ProgrammaticSortMode 时,必须通过 DataGridViewColumnHeaderCell.SortGlyphDirection 属性自行显示排序字形。

注意

使用自定义排序对控件进行排序时,此属性的值没有意义。 有关自定义排序的详细信息,请参阅 Sort(IComparer) 方法和 SortCompare 事件。

适用于

另请参阅