DataGridView.SortOrder DataGridView.SortOrder DataGridView.SortOrder DataGridView.SortOrder Property

定义

获取一个值,该值指示是按升序或降序对 DataGridView 控件中的项进行排序,还是不排序。Gets a value indicating whether the items in the DataGridView control are sorted in ascending or descending order, or are not sorted.

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

属性值

SortOrder 值之一。One of the SortOrder values.

示例

下面的代码示例演示如何在编程排序SortOrder中使用属性。The following code example demonstrates how to use the SortOrder 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

注解

此属性用于确定当SortedColumn属性指定的列SortMode具有的Automatic属性值时,将显示哪种排序标志符号。This property is used to determine which sorting glyph appears when the column specified by the SortedColumn property has a SortMode property value of Automatic. 当列SortMode的属性值为Programmatic时,必须通过SortGlyphDirection属性自行显示和隐藏排序标志符号。When the column has a SortMode property value of Programmatic, you must display and hide the sorting glyph yourself through the SortGlyphDirection property. 如果列SortMode的属性NotSortable值为,则可以显示排序标志符号,但如果列自动调整大小,则不会为它保留空间。When the column has a SortMode property value of NotSortable, you can display the sorting glyph, but space is not reserved for it if the column is automatically resized.

备注

使用自定义排序对控件进行排序时,此属性的值没有意义。The value of this property is not meaningful when you sort the control using custom sorting. 有关自定义排序的详细信息,请Sort参阅方法SortCompare和事件。For more information about custom sorting, see the Sort method and the SortCompare event.

适用于

另请参阅