DataGridView.SelectedColumns 속성

정의

사용자가 선택한 열의 컬렉션을 가져옵니다.Gets the collection of columns selected by the user.

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

속성 값

사용자가 선택한 열을 나타내는 DataGridViewSelectedColumnCollection입니다.A DataGridViewSelectedColumnCollection that represents the columns selected by the user.

특성

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 SelectedColumns 프로그래밍 방식으로 정렬에서 선택된 된 열을 가져올 속성입니다.The following code example demonstrates how to use the SelectedColumns property to get the selected column in a programmatic sort. 이 예제에서는이 속성은에 사용 된 Sort 정렬 방향을 결정 하는 방법.In this example, this property is used in the Sort method to determine the direction of the 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

설명

SelectionMode 속성 설정 해야 합니다 DataGridViewSelectionMode.FullColumnSelect 또는 DataGridViewSelectionMode.ColumnHeaderSelect 에 대 한는 SelectedColumns 속성을 채울 열을 선택 합니다.The SelectionMode property must be set to DataGridViewSelectionMode.FullColumnSelect or DataGridViewSelectionMode.ColumnHeaderSelect for the SelectedColumns property to be populated with selected columns.

이 속성에는 선택 항목은 참조 될 때에는 읽기 전용 스냅숏을 포함 합니다.This property contains a read-only snapshot of the selection at the time it is referenced. 이 컬렉션의 복사본을 보유 하는 경우 달라질 수 있습니다 실제에서 후속 DataGridView 상태는 사용자 변경한 선택 합니다.If you hold onto a copy of this collection, it may differ from the actual, subsequent DataGridView state in which the user may have changed the selection. 따라서 컬렉션의 복사본에 대해 있습니다 해야 작동 하는 것은 없습니다.Therefore, you should not operate on a copy of the collection.

적용 대상

추가 정보