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

설명

이 속성은 속성에 지정된 SortedColumnSortMode 열에 속성 값 Automatic이 있을 때 표시되는 정렬 문자 모양을 결정하는 데 사용됩니다. 열의 속성 값ProgrammaticSortMode 인 경우 속성을 통해 SortGlyphDirection 정렬 문자 모양을 직접 표시하고 숨겨야 합니다. 열의 속성 값NotSortableSortMode 인 경우 정렬 문자 모양을 표시할 수 있지만 열의 크기가 자동으로 조정되는 경우에는 공백이 예약되지 않습니다.

참고

사용자 지정 정렬을 사용하여 컨트롤을 정렬할 때 이 속성의 값은 의미가 없습니다. 사용자 지정 정렬에 대한 자세한 내용은 메서드 및 SortCompare 이벤트를 참조 Sort 하세요.

적용 대상

추가 정보