[英]DataGridViewCheckBoxColumn - If checkBox checked issue
I have this code down below which should show messageBox when the checkbox in checkbox column is checked. 我将这段代码放到下面,当选中“复选框”列中的复选框时,该代码应显示messageBox。 It is test for me that I know that the row was really selected.
对我而言,我知道该行确实已被选中是一个考验。
If this would work I'm going to save SelectedRows into DB. 如果这行得通,我将把SelectedRows保存到数据库中。 So maybe its helpful to know when building this code.
因此,在构建此代码时了解它可能会有所帮助。 As I'm begginer I wanted to ask you guys why MessageBox doesnt apper when I check checkBox?
因为我是初学者,所以我想问大家,当我选中checkBox时,为什么MessageBox没有包装? Thanks so much in advance.
非常感谢。
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
dtg_ksluzby.Columns.Add(chk);
dtg_ksluzby.Columns[3].Width = 20;
foreach (DataGridViewRow row in dtg_ksluzby.Rows)
{
// number 3 represents the 4th column of dgv
DataGridViewCheckBoxCell chk1 = row.Cells[3] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chk1.Value) == true)
{
MessageBox.Show("this cell checked");
}
else
{
}
}
This code will never hit the message box code - you've created the control, added it to the table, then immediately checked them for their values, which will be not set. 此代码将永远不会触及消息框代码-您已经创建了控件,将其添加到表中,然后立即检查它们的值(未设置)。
You need to have an event handler that catches changed values in the datagridview: 您需要有一个事件处理程序,可以在datagridview中捕获更改的值:
private void dtg_ksluzby_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
// Check through the cells here (or use event args to get data)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.