Acting on Checkbox click on datagridview...

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
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();
                }
            }
        }
 
Selected and Checked are two different things.
 
So, followed this line of thinking a bit more and was able to use .Value. Had to convert to boolean to read it, but then it set just fine. So, resolved. Apparently, I just had to sleep on it!
 
Back
Top Bottom