Datagridview按钮单击事件 [英] Datagridview button click event
问题描述
大家好,
谁能帮我,我遇到问题了
访问按钮的点击事件
在我的datagridview中?
任何帮助都将得到应用!
Hi all,
Can anyone help me, I''m having problems
accessing the click event of the button/s
in my datagridview?
Any help would be appriciated!!
推荐答案
尝试:
...
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
...
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
DataGridViewButtonCell b = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewButtonCell;
}
}
检查RowIndex
和ColumnIndex
的有效性可能是一个好主意,但这为您提供了这个主意.
It would probably be a good idea to check the RowIndex
and ColumnIndex
for validity, but that gives you the idea.
我的下一篇文章-这是摘录(我应该准备完整的文章准备明天提交),我已经在设计时使用IDE在我的DataGridView中添加了一个命令按钮-称为dgLoadImage.列类型的DataGridViewButtonColumn将为您创建按钮.为了使用该按钮,我已经将CellContentClick事件添加到了DataGridView中.乍一看,此事件的代码看起来有些奇怪,因为它以if语句打开,当click事件位于按钮以外的任何列上时,该if语句会强制退出
In the example from my next article - here is an extract (I should have the complete article ready for submission tomorrow), I have used the IDE to add a command button to my DataGridView at design time - called dgLoadImage. A column type of DataGridViewButtonColumn will create the button for you. In order to use the button I have added a CellContentClick event to my DataGridView. At first glance the code for this event looks a little bizarre as it opens with an if statement that forces exit when the click event is on any column other than the button
if (e->ColumnIndex != dgLoadImage->Index)
return; // Hardcoded to only accept the event on the button column
else
{
:
}
这只是我格外小心. CellContentClick事件只会对按钮列触发,因此在此示例中,我无需担心,但是,如果我的表单上有多个按钮,则需要确定哪个列触发了click事件并采取相应的行动.
This is just me being extra carefull. The CellContentClick event will only fire for button columns, so in this example I have nothing to worry about, however if I had more than one button on my form, then I need to determine which column triggered the click event and act accordingly.
这篇关于Datagridview按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!