DataGridView.SortedColumn 속성

정의

DataGridView 내용의 현재 정렬 기준이 되는 열을 가져옵니다.

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

속성 값

DataGridViewColumn 내용의 현재 정렬 기준이 되는 DataGridView입니다.

특성

예제

다음 코드 예제를 사용 하는 방법을 보여 줍니다는 SortedColumn 프로그래밍 방식으로 정렬에서 속성입니다.

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

설명

DataGridView 정렬되지 않으면 이 속성은 를 반환 null합니다.

이 속성으로 표시된 열의 속성 값이 SortMode 인 경우 속성 값 DataGridViewColumnSortMode.Automatic에 따라 정렬 문자 모양이 SortOrder 표시됩니다.

열의 속성 값DataGridViewColumnSortMode.Programmatic이 인 SortMode 경우 속성을 통해 DataGridViewColumnHeaderCell.SortGlyphDirection 정렬 문자 모양을 직접 표시해야 합니다.

참고

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

적용 대상

추가 정보