0 out of 1 rated this helpful - Rate this topic

DataGridView.ColumnHeaderMouseClick Event

Occurs when the user clicks a column header.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public event DataGridViewCellMouseEventHandler ColumnHeaderMouseClick

For more information about handling events, see Consuming Events.

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. 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. In this example, the SelectionMode is set to ColumnHeaderSelect, so clicking the DataGridViewColumnHeaderCell selects the contents of the column. 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. Add a DataBindingComplete event handler to provide the code that changes the default behavior. 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;
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
As the dummy I am, ... (Continued)
3. (For Gurus, Pops, MCs and those who think they are ...)
   If you do it, review it befor publishing!

Sorry for the Glyph brothers (Hyram, Hyro and the anonymos working at Microsoft)!

Private Sub DGR_ColumnHeaderMouseClick(
    ByVal sender As Object, _
    ByVal e As DataGridViewCellMouseEventArgs)

    Dim _
        direction As ListSortDirection = ListSortDirection.Ascending

        With myDataGrid

            If Not .SortedColumn Is Nothing Then

                If .SortedColumn Is .Columns(e.ColumnIndex) Then

                    If .SortOrder = SortOrder.Ascending Then

                        direction = ListSortDirection.Descending

                    End If
                End If
            End If

            .Sort(myDataGrid.Columns(e.ColumnIndex), direction)

         End With
    End Sub
As the dummy I am, ...
Lokking at the examples of MSDN, I always remeber Michael A. Jackson's "rules for when to optimize":
1. Don't do it!
2. (for experts only)
   Don't do it yet!

So, trying to understand the example above, translating it for my limited possibilities, I came to:

Private Sub DGR_ColumnHeaderMouseClick(
        ByVal sender As Object, _
        ByVal e As DataGridViewCellMouseEventArgs) Handles myDataGrid.ColumnHeaderMouseClick

        Dim _
            direction As ListSortDirection = ListSortDirection.Ascending

        With myDataGrid

            ' if the DataGridView is already sorted

            If Not .SortedColumn Is Nothing Then

                ' reset the little indicator in the header of the sorted cell

                .SortedColumn.HeaderCell.SortGlyphDirection = SortOrder.None

                ' if selected colums is already the ordered column

                If .SortedColumn Is .Columns(e.ColumnIndex) Then

                    ' reverse sort order ('Ascending' is default

                    If .SortOrder = SortOrder.Ascending Then

                        direction = ListSortDirection.Descending

                    End If
                End If
            End If

            ' Sort the selected column.

            .Sort(myDataGrid.Columns(e.ColumnIndex), direction)

            ' and set the little indicator in the header of the sorted cell

            .Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = .SortOrder

        End With
    End Sub

    Private Sub DGR_DataBindingComplete(
        ByVal sender As Object, _
        ByVal e As DataGridViewBindingCompleteEventArgs) Handles myDataGrid.DataBindingComplete

        ' Put each of the columns into programmatic sort mode.

        For Each column As DataGridViewColumn In myDataGrid.Columns

            column.SortMode = DataGridViewColumnSortMode.Programmatic

        Next
    End Sub
DataGridView.ColumnHeaderMouseClick Event never fires
Using VS 2008 and .NET Framework 3.5, this event never fires - if I click on a column header, I get instead a 
tAccountDataGridView_CellContentClick event with e.RowIndex = -1 - If I click on the RowHeader/Column Header cell (0,0), no event fires at all

private void tAccountDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
Console.WriteLine("Our Mouse Coordinates are row {0}, column {1}", e.RowIndex, 0);
tAccount2DataGridView.Rows[e.RowIndex].Selected = true;
tAccount2DataGridView.CurrentCell = tAccount2DataGridView.Rows[e.RowIndex].Cells[1];
tAccount3DataGridView.Rows[e.RowIndex].Selected = true;
tAccount3DataGridView.CurrentCell = tAccount3DataGridView.Rows[e.RowIndex].Cells[1];
}