C#: Create click event for DataGridView column heading

NickB6 picture NickB6 · Apr 4, 2013 · Viewed 8.6k times · Source

I am trying to create a click event for a DataGridView column headers so that when the user clicks to reorder the data I can run code to draw the gantt chart in the new order (see screen shot below).

As you can see I want the user to be able to sort the datagridview and the gantt chart to respond to the re-ordering.

I have tried using the click event as per msdn, however this had no effect when i tested it: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columnheadermouseclick.aspx

private void dataCapPlan_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        MessageBox.Show("OK");
    }

for DataGridView dataCapPlan

Any Ideas?

Much Appreciated!

Form

Answer

NickB6 picture NickB6 · Apr 4, 2013

Needed to add code:

dataCapPlan.ColumnHeaderMouseClick +=new DataGridViewCellMouseEventHandler(dataCapPlan_ColumnHeaderMouseClick);

see post: Event for clicking on row headers in DataGridView