DataGridView.SortOrder 屬性

定義

取得值,指出 DataGridView 控制項中的項目是以遞增或遞減順序排序,或者根本不排序。

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

屬性值

其中一個 SortOrder 值。

屬性

範例

下列程式碼範例示範如何在程式設計排序中使用 SortOrder 屬性。

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 時,這個屬性會用來判斷出現的排序圖像。 當資料行的屬性值 ProgrammaticSortMode 時,您必須透過 屬性自行 SortGlyphDirection 顯示和隱藏排序圖像。 當資料行的屬性值 NotSortableSortMode 時,您可以顯示排序圖像,但如果資料行自動調整大小,則不會保留空間給它。

注意

當您使用自訂排序來排序控制項時,這個屬性的值並不有意義。 如需自訂排序的詳細資訊,請參閱 Sort 方法和 SortCompare 事件。

適用於

另請參閱