DataGridView.SortedColumn DataGridView.SortedColumn DataGridView.SortedColumn DataGridView.SortedColumn Property

정의

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.

열이이 속성에 나타난 경우에 SortMode 속성 값이 DataGridViewColumnSortMode.Automatic의 값을 기준으로 정렬 문자 모양을 표시 됩니다는 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.

인 열의 경우는 SortMode 속성 값 DataGridViewColumnSortMode.Programmatic를 통해 직접 정렬 문자를 표시 해야 하는 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.

적용 대상

추가 정보