如何响应gridview的特定按钮事件 [英] how to respond to gridview's specific button event
本文介绍了如何响应gridview的特定按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一行网格视图。 Row包含Combobox文本框和两个按钮。
现在我想处理在gridview里面的Button事件我正在使用它。
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 .
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);
}
工作正常,但问题是;它正在处理按钮单击事件。另外,当我尝试在文本框中写入时(在gridview内部),它也会起火。
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.
推荐答案
您可以通过列索引进行限制:如果您的按钮位于第五列,例如,它的索引是4.所以,而不是:
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:
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
你可以写:
you can write:
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.
这篇关于如何响应gridview的特定按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文