Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a a gridview with one row. Row Contain Combobox text box and two button.
now i want to work on Button event which is inside of gridview i am using this .
C#
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
   {
      DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
      dataGridView1.Rows.Add(row);

      MessageBox.Show(e.ColumnIndex.ToString()+' '+e.RowIndex);
   }

It working fine but the problem is; it is working on both button click event. plus as i try to write in textbox (which inside of gridview) it also get fire.
Posted
Updated 26-Oct-13 8:05am
v4
Comments
phil.o 26-Oct-13 14:06pm    
What is the name of the method this code resides in?
Muhamad Faizan Khan 26-Oct-13 23:20pm    
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

1 solution

You could restrict by the column index: if your button is on the fifth column, for example, it's index is 4. So, instead of:
C#
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))

you can write:
C#
if (e.RowIndex >= 0 && e.ColumnIndex == 4)

With the inconvenient that you have to change your code whenever you modify the index of the column.
 
Share this answer
 
Comments
Muhamad Faizan Khan 27-Oct-13 12:38pm    
this is fine but when select any item in combobox or i type in textbox another row appears/ i mean this event get fire why??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900