DataGridView.ColumnHeaderMouseClick Evento

Definición

Se produce cuando el usuario hace clic en un encabezado de columna.Occurs when the user clicks a column header.

public:
 event System::Windows::Forms::DataGridViewCellMouseEventHandler ^ ColumnHeaderMouseClick;
public event System.Windows.Forms.DataGridViewCellMouseEventHandler ColumnHeaderMouseClick;
member this.ColumnHeaderMouseClick : System.Windows.Forms.DataGridViewCellMouseEventHandler 
Public Custom Event ColumnHeaderMouseClick As DataGridViewCellMouseEventHandler 

Ejemplos

Cuando se hace clic en un encabezado de columna en un DataGridView, el comportamiento predeterminado es ordenar las filas de la cuadrícula en función de la columna en la que se ha hecho clic, o bien invertir el criterio de ordenación si la cuadrícula ya está ordenada por la columna en la que se ha hecho clic.When a column heading is clicked in a DataGridView, the default behavior is to order the grid rows based on the clicked column, or to reverse the sort order if the grid is already sorted by the clicked column. En el ejemplo de código siguiente se muestra cómo utilizar este evento para realizar una ordenación mediante programación que emula el comportamiento predeterminado de hacer clic en un DataGridViewColumnHeaderCell cuando se ha deshabilitado el comportamiento predeterminado.The following code example demonstrates how to use this event to perform a programmatic sort that emulates the default behavior of clicking a DataGridViewColumnHeaderCell when the default behavior has been disabled. En este ejemplo, el SelectionMode se establece en ColumnHeaderSelect, por lo que al hacer clic en el @no__t 2 se selecciona el contenido de la columna.In this example, the SelectionMode is set to ColumnHeaderSelect, so clicking the DataGridViewColumnHeaderCell selects the contents of the column. Para que el código de ejemplo funcione según lo previsto, es necesario cambiar el comportamiento de clic de encabezado de columna predeterminado cada vez que los datos se cargan en el DataGridView.For the example code to function as intended, the default column header click behavior needs to be changed each time the data is loaded into the DataGridView. Agregue un controlador de eventos DataBindingComplete para proporcionar el código que cambia el comportamiento predeterminado.Add a DataBindingComplete event handler to provide the code that changes the default behavior. Para ejecutar este ejemplo, pegue el código en un formulario que contenga un DataGridView denominado dataGridView1 y asegúrese de que todos los eventos estén asociados a sus controladores de eventos.To run this example, paste the code into a form that contains a DataGridView named dataGridView1 and ensure that all events are associated with their event handlers.

private void dataGridView1_ColumnHeaderMouseClick(
    object sender, DataGridViewCellMouseEventArgs e)
{
    DataGridViewColumn newColumn = dataGridView1.Columns[e.ColumnIndex];
    DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
    ListSortDirection direction;

    // If oldColumn is null, then the DataGridView is not 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;
    }

    // Sort the selected column.
    dataGridView1.Sort(newColumn, direction);
    newColumn.HeaderCell.SortGlyphDirection =
        direction == ListSortDirection.Ascending ?
        SortOrder.Ascending : SortOrder.Descending;
}

private void dataGridView1_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
{
    // Put each of the columns into programmatic sort mode.
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        column.SortMode = DataGridViewColumnSortMode.Programmatic;
    }
}
Private Sub dataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, _
    ByVal e As DataGridViewCellMouseEventArgs) _
    Handles dataGridView1.ColumnHeaderMouseClick

    Dim newColumn As DataGridViewColumn = _
        dataGridView1.Columns(e.ColumnIndex)
    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

    ' Sort the selected column.
    dataGridView1.Sort(newColumn, direction)
    If direction = ListSortDirection.Ascending Then
        newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
    Else
        newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
    End If

End Sub

Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, _
    ByVal e As DataGridViewBindingCompleteEventArgs) _
    Handles dataGridView1.DataBindingComplete

    ' Put each of the columns into programmatic sort mode.
    For Each column As DataGridViewColumn In dataGridView1.Columns
        column.SortMode = DataGridViewColumnSortMode.Programmatic
    Next
End Sub

Comentarios

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.For more information about how to handle events, see Handling and Raising Events.

Se aplica a

Consulte también: