Reputation: 3575
I have this code down below which should show messageBox when the checkbox in checkbox column is checked. 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. 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? 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
{
}
}
Upvotes: 1
Views: 11298
Reputation: 370
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:
private void dtg_ksluzby_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
// Check through the cells here (or use event args to get data)
}
Upvotes: 1