DataGridView.SortOrder 속성

정의

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 속성으로 지정 된 열에 AutomaticSortMode 속성 값이 있는 경우 표시 되는 정렬 문자 모양을 결정 하는 데 사용 됩니다.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. 열에 ProgrammaticSortMode 속성 값이 있는 경우 SortGlyphDirection 속성을 통해 정렬 문자 모양을 직접 표시 하 고 숨겨야 합니다.When the column has a SortMode property value of Programmatic, you must display and hide the sorting glyph yourself through the SortGlyphDirection property. 열에 NotSortableSortMode 속성 값이 있는 경우 정렬 문자 모양을 표시할 수 있지만 열 크기를 자동으로 조정 하는 경우에는 공간이 예약 되지 않습니다.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.

적용 대상

추가 정보