DataGridView.SortOrder Propriedade

Definição

Obtém um valor que indica se os itens do controle DataGridView são classificados em ordem crescente ou decrescente, ou se não são classificados.

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

Valor da propriedade

Um dos valores de SortOrder.

Atributos

Exemplos

O exemplo de código a seguir demonstra como usar a SortOrder propriedade em uma classificação programática.

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

Comentários

Essa propriedade é usada para determinar qual glifo de classificação aparece quando a coluna especificada pela SortedColumn propriedade tem um SortMode valor de propriedade de Automatic. Quando a coluna tiver um SortMode valor de propriedade de Programmatic, você deverá exibir e ocultar o glifo de classificação por conta própria por meio da SortGlyphDirection propriedade . Quando a coluna tiver um SortMode valor de propriedade de NotSortable, você poderá exibir o glifo de classificação, mas o espaço não será reservado para ela se a coluna for redimensionada automaticamente.

Observação

O valor dessa propriedade não é significativo quando você classifica o controle usando a classificação personalizada. Para obter mais informações sobre classificação personalizada, consulte o Sort método e o SortCompare evento .

Aplica-se a

Confira também