Rezise DataGridViewColumn at runtime

The event to use is ColumnDividerDoubleClick. Yes this works BUT there is one problem.
If you use the event DoubleClick (or similar) then it is possible that you will not will not see the ColumnDividerDoubleClick event triggered.
The next code while provide a possible problem

 
private void dgv_ColumnDividerDoubleClick(object sender,
                                 DataGridViewColumnDividerDoubleClickEventArgs e)
{
}
private void dgv_DoubleClick(object sender, EventArgs e)
{
}

Changes are that both events are triggered or the wrong one.

A way to catch this is by checking the cursor handle of the sender. Here a way to do this:

 
private void dgv_ColumnDividerDoubleClick(object sender,
                                 DataGridViewColumnDividerDoubleClickEventArgs e)
{
   DataGridView _dgv = (DataGridView)sender;
   if ( _dgv.Cursor.Handle.ToInt32() == 65553) 
      DataGridView1.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.AllCells);
}
private void dgv_DoubleClick(object sender, EventArgs e)
{
   DataGridView _dgv = (DataGridView)sender;
   if ( _dgv.Cursor.Handle.ToInt32() == 65539) 
      this.openDetails();
}

This is the trigger order. If you use a other event just create them all and test to see the order and the values, maybe more checks are needed.

1.) DoubleClick has Cursor.Handle = 65539
2.) CellDoubleClick has Cursor.Handle = 65539
3.) ColumnDividerDoubleClick has Cursor.Handle = 65553

HTH

Leave a Reply