My Datagridview displays fine with a check box column tacked on the end. I need to ACT on my checkbox click though. I am trying something like the following but it never hits the false section. All I get is true. I need allow the user to check the box (and then act on it), but block an Uncheck.
C#:
private void dgvCOB_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dgvCOB.EndEdit(); //Trying to end edit mode and "Post" the value??
if (e.ColumnIndex.ToString() == "6") //Checking the column of the cell clicked. 6 is the column with the checkbox.
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgvCOB.Rows[e.RowIndex].Cells[e.ColumnIndex]; //grab the value of the current entry
if (chk.Selected == true)
{
//chk.Selected = false; //This was in an example. Not sure what the purpose is.
MessageBox.Show("We are checked");
}
else
{
//chk.Selected = false; //This was in an example. Not sure what the purpose is.
MessageBox.Show("You cannot clear a check box.");
dgvCOB.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true; //Set the state of the checkbox back to checked.
this.Refresh();
}
}
}