0

I have a dataGridView and I am trying to fire an event when a cell is clicked.I have tried using the following events with no avail:

    private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    { 
     //dosomething
    }

    private void dataGridView2_CellMouseClick(object sender, DataGridViewCellEventArgs e)
    { 
     //dosomething 
    }

    private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
    { 
     //dosomething 
    }

    private void dataGridView2_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    { 
     //dosomething 
    }

    private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    { 
     //dosomething 
    }


I made sure the dataGridView's name matched, but none of these events are firing or showing up in the debugger. Am I missing something?

firebox
  • 41
  • 5
  • 1
    Matching the name is not important, make sure you attached the event handler method to the event using code or using designer. – Reza Aghaei Jan 27 '17 at 21:46
  • 2
    I think you're missing the part where you actually hook up the events. It doesn't magically attach the event because you have the name of the control, an underscore, then the event name. Like @RezaAghaei suggested, use the designer or do it in code. – KSib Jan 27 '17 at 21:47
  • Odd, I've never had to hook it up before. I've always typed it out and it has just worked. – firebox Jan 27 '17 at 21:50
  • 1
    _I've always typed it out and it has just worked._ nonsense! See [here](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=14|0.0000#33276161) to learn more about hooking up events! – TaW 5 mins ago – TaW Jan 27 '17 at 22:27
  • It may be nonsense to you, but I have not had to hook up the event before, and I have been using C# for 4-5 years. – firebox Jan 28 '17 at 03:36
  • You were clearly missing something as it is impossible for an event to fire without being hooked to a handler one way or another. – J. Rockwood Feb 02 '22 at 15:56

1 Answers1

1

Select the grid in designer, goto properties, click events button in the properties and double click on the value cell on the right of CellClick event OR Goto designer.cs file and look where all properties of gridview are defined and see if any line like this is present

gridview.CellClick += new EventHandler(dataGridView2_CellClick);

If it isnt present then add it.