DataGridView.SelectedColumns DataGridView.SelectedColumns DataGridView.SelectedColumns DataGridView.SelectedColumns Property

Definición

Obtiene la colección de columnas seleccionadas por el usuario.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

Valor de propiedad

Ejemplos

En el ejemplo de código siguiente se muestra cómo SelectedColumns utilizar la propiedad para obtener la columna seleccionada en una ordenación mediante programación.The following code example demonstrates how to use the SelectedColumns property to get the selected column in a programmatic sort. En este ejemplo, esta propiedad se utiliza en el Sort método para determinar la dirección de la ordenación.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

Comentarios

La SelectionMode propiedad se debe establecer en DataGridViewSelectionMode.FullColumnSelect o DataGridViewSelectionMode.ColumnHeaderSelect para que SelectedColumns la propiedad se rellene con las columnas seleccionadas.The SelectionMode property must be set to DataGridViewSelectionMode.FullColumnSelect or DataGridViewSelectionMode.ColumnHeaderSelect for the SelectedColumns property to be populated with selected columns.

Esta propiedad contiene una instantánea de solo lectura de la selección en el momento en que se hace referencia a ella.This property contains a read-only snapshot of the selection at the time it is referenced. Si mantiene una copia de esta colección, puede diferir del estado real y subsiguiente DataGridView en el que el usuario puede haber cambiado la selección.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. Por lo tanto, no debe trabajar en una copia de la colección.Therefore, you should not operate on a copy of the collection.

Se aplica a

Consulte también: