7

I have a datagridview in which one of the columns is a checkbox. I handle the CellContentClick event to update information everytime the user check or uncheck one of the checkboxes. It works great. My problem is that when I double click a checkbox CellContentClick is called and then CellContentDoubleClick after that. I want to annul the call for CellContentDoubleClick. Is there a way to do this?

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
user990692
  • 543
  • 1
  • 8
  • 16
  • You could add a flag in the class that the click is being handled, and return in the double click event if that flag is set. but that highly depends on the order the events are sent, and is probably not the best way of doing it. – cyanic Feb 05 '12 at 23:06
  • Thanks. Yeah that seems like a good option. However, I don't know if when I do a **return** the checkbox will endup marked or not anyways. One thing I did is set the property **ReadOnly** true, after this when you double click the checkbox in the cell is only updated once. – user990692 Feb 05 '12 at 23:19
  • 1
    You could easily use a bool flag to ignore the event when it comes very shortly after the CellContentClick. I'm starting to recall this as a bug... Here is some more info: http://www.aspnet-answers.com/microsoft/NET/33946836/datagridview-cellcontentclick-fires-after-celldoubleclick.aspx and http://stackoverflow.com/questions/381072/datagridview-cellcontentclick and http://social.msdn.microsoft.com/Forums/en-us/csharpgeneral/thread/e6f3a286-3e93-4170-a895-8c68a616cc37 – Jeremy Thompson Feb 06 '12 at 04:26

4 Answers4

1

You can remove event handler from datagrid.

     EventHandler eventHandler = new EventHandler(YourdataGridview_CellContentDoubleClick);
     YourdataGridview.CellContentDoubleClick -= eventHandler; 
David Jazbec
  • 167
  • 4
0

How about this:

public class MyDataGridView : DataGridView
{
    protected override void OnCellContentDoubleClick(DataGridViewCellEventArgs e)
    {
        base.OnCellContentClick(e);
    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
0

Instead of annulling for CellContentDoubleClick you can wire CellContentClick and CellContentDoubleClick to a single method.

            gridviewTreasures.CellContentClick += new DataGridViewCellEventHandler(gridviewTreasures_CellContentClick);
            gridviewTreasures.CellContentDoubleClick += new DataGridViewCellEventHandler(gridviewTreasures_CellContentClick);
0

You could create your own class which inherits from DataGridView and override the method which would raise the event so that it doesn't get raised.

public class MyDataGridView : DataGridView
{
    protected override viod OnCellContentDoubleClick(
DataGridViewCellEventArgs e)
    {
        // by having no code here and not 
        // calling base.OnCellContentDoubleClick(e);
        // you prevent the event being raised
    }
}

See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.oncellcontentdoubleclick.aspx

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60