Link to home
Start Free TrialLog in
Avatar of adamtrask
adamtrask

asked on

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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Add checks within the event handler to find out which column was clicked to determine the button that was clicked.
Avatar of adamtrask
adamtrask

ASKER

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

thanks
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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....
Did you set IndexOfDelete to some value? It needs to be column index of the delete button.
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
I finally got it, sorry for being a bit slow.

thanks