capture click of column header of DataGrid

  • Thread starter Thread starter ewillyb
  • Start date Start date
E

ewillyb

Would like to detect when a column header of a DataGrid is
double-clicked. I'm really puzzled why there is not an obvious way to
differentiate between column, row, cell clicks of a DataGrid.

Please note that I have looked at the syncfusion.com/faq/winforms
article which recommends using the HitTestInfo class. I implemented
this solution and the column header click is not being recognized.
The odd thing is that my intellisense does not recognize the
HitTestInfo or HitTestType classes, and the code compiles! What is
going on here?

Does the event have to be a mouse down or can I use dbl-click?

Finally, if someone has a better way to solve the root problem, then
I'm all ears. Basically, I need a double click of a row header or
cell to fire an event that deals with the specific row. BUT, I do not
want a column header dbl-click to do anything.

Thx,
EWB
 
It sounds like you were looking in the right direction.

This works fine for me (hook into the MouseDown of your DataGrid or subclass
and override the OnMouseDown):

private void dataGrid1_MouseDown(object sender, MouseEventArgs e)
{
DataGrid.HitTestInfo Info = dataGrid1.HitTest(e.X, e.Y);
if (e.Clicks == 2 && Info.Type == DataGrid.HitTestType.ColumnHeader)
{
MessageBox.Show("ColumnHeader DoubleClick");
}
else
{
System.Diagnostics.Debug.WriteLine("Clicks: " + e.Clicks + ", At: ("
+ Info.Row + ", " + Info.Column + ") on " + Info.Type.ToString());
}
}
 
Back
Top