DataGridView.SortedColumn 속성

정의

DataGridView 내용의 현재 정렬 기준이 되는 열을 가져옵니다.Gets the column by which the DataGridView contents are currently sorted.

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

속성 값

DataGridViewColumn 내용의 현재 정렬 기준이 되는 DataGridView입니다.The DataGridViewColumn by which the DataGridView contents are currently sorted.

특성

예제

다음 코드 예제에서는 프로그래밍 방식 정렬에서 SortedColumn 속성을 사용 하는 방법을 보여 줍니다.The following code example demonstrates how to use the SortedColumn 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

설명

DataGridView 정렬 되지 않은 경우이 속성은 null을 반환 합니다.If the DataGridView is not sorted, this property will return null.

이 속성이 나타내는 열에 DataGridViewColumnSortMode.AutomaticSortMode 속성 값이 있는 경우 SortOrder 속성의 값을 기준으로 정렬 문자 모양이 표시 됩니다.When the column indicated by this property has a SortMode property value of DataGridViewColumnSortMode.Automatic, it will display a sorting glyph based on the value of the SortOrder property.

열에 DataGridViewColumnSortMode.ProgrammaticSortMode 속성 값이 있는 경우 DataGridViewColumnHeaderCell.SortGlyphDirection 속성을 통해 정렬 문자 모양을 직접 표시 해야 합니다.When the column has a SortMode property value of DataGridViewColumnSortMode.Programmatic, you must display the sorting glyph yourself through the DataGridViewColumnHeaderCell.SortGlyphDirection property.

참고

사용자 지정 정렬을 사용 하 여 컨트롤을 정렬 하는 경우에이 속성의 값은 의미가 없습니다.The value of this property is not meaningful when you sort the control using custom sorting. 사용자 지정 정렬에 대 한 자세한 내용은 Sort(IComparer) 메서드 및 SortCompare 이벤트를 참조 하세요.For more information about custom sorting, see the Sort(IComparer) method and the SortCompare event.

적용 대상

추가 정보