We help IT Professionals succeed at work.

adding more than one column button to a dataGridView

Hi,

I am using an unbound dataGridView to display data from an sql database table.  
I tried experimenting by adding a column button to the dataGridView. I did this using the Add column option in the dataGridView Tasks.

With this I created the first column button, and by double clicking that button arrived at the
dataGridView1_CellContentClick which I used to  add code that deletes the selected row.

When I tried to create a second column button I found that by double clicking on it I arrive at the above event, i.e the same dataGridView1_CellContentClick

Obviously I could not use the same event to execute different tasks (like updating and deleting).

My question is this: Are we only allowed to create a single column button for a dataGridView or am I doing some thing wrong....?

Thanks
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
Add checks within the event handler to find out which column was clicked to determine the button that was clicked.

Author

Commented:
Not quite sure how to do that CodeCruiser...
Please show me.,

thanks
Most Valuable Expert 2012
Top Expert 2014
Commented:
Try

If e.ColumnIndex = IndexOfButton1 Then
   'do button 1 stuff
ElseIf e.ColumnIndex = IndexOfButton2 Then
  'do button 2 stuff
...
End If

Author

Commented:
Still can't figure it out.... The two buttons I have are named Delete and Update and their HeaderText are given the same names....

I tried:
  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == IndexOfDelete)
And:

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex ==dataGridView1.Delete)

Same errors... obviously this is not the correct way of doing it....
Most Valuable Expert 2012
Top Expert 2014

Commented:
Did you set IndexOfDelete to some value? It needs to be column index of the delete button.

Author

Commented:
CodeCruiser:

Sorry for not responding. I was trying too many things and ended up messing up the form or many of the controls on the form... I have too many errors to fix.

As for assigning a value to the index of the Delete column Button the answer is no. Simply because I created that button using the Add column option on the dataGridView Tasks, and I didn't see an indexOf property any where

Author

Commented:
I finally got it, sorry for being a bit slow.

thanks