如何知道 datagridview 中的特定复选框是否被选中? [英] How to know a specific checkbox inside datagridview is checked or not?
问题描述
我有一个 gridview,它有 2 列,一个是文本框列,另一个是复选框列,如何知道哪个复选框被选中.
i had a gridview which has 2 columns , one is textbox column and other is checkbox column, how to know which checkbox is checked .
如图所示,假设任何复选框被选中,我想显示该复选框对应的文本框值.
As shown in image ,suppose any of the checkbox is checked , i want to display that the corresponding text box value to that checkbox.
有人可以帮我吗?我尝试了下面的代码,但我面临的问题是,一旦我点击下一个复选框,这些值就会显示出来,然后显示之前选中的复选框值..
can anyone help me?i tried the below code , but problem which i am facing is that , the values is getting displayed once i clicked to next checkbox then the previously checked checkbox values is getting displayed..
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
dataGridView1_CurrentCellDirtyStateChanged(sender, e);
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
推荐答案
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
下面的这些链接帮助我理解了 cellvalue_changed 和 cell_content_click 的概念.http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
these below links helped me to understand the concept of cellvalue_changed and cell_content_click.. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
在这些链接的帮助下,我终于找到了解决问题的方法
and by the help of these links i finally got the solution to my problem
这篇关于如何知道 datagridview 中的特定复选框是否被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!