Resolved DataGridView Specific Row set to CheckBox - How to check the state of the CheckBox

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
58
Programming Experience
10+
Hi,
Thanks to help in this forum, I got the CheckBox working visually. How do I check what the state of the CheckBox is? It seems to behave very strangely. I had looked at several values including "Value" and "EditedFormattedValue" and non appears to work correctly. "EditedFormattedValue" seems to work opposite and "Value" never changes from "false" no matter the physical state of the CheckBox. Below is the DataGridViewCheckBoxCell definition and the second section is the DataGridView_CellMouseClick routine.

C#:
dgvQuery.Columns.Add("Column" + e.Index, clbFPYFields.SelectedItem.ToString());
if (dgvQuery.Columns.Count == 1)
{
    dgvQuery.Rows.Add();
    DataGridViewComboBoxCell cbCell = new DataGridViewComboBoxCell();
    cbCell.Items.Add("Ascending");
    cbCell.Items.Add("Descending");
    cbCell.Items.Add("[not sorted]");
    dgvQuery[0, 0] = cbCell;
    dgvQuery[0, 0].Value = "[not sorted]";
    dgvQuery.Rows[0].HeaderCell.Value = "Sort";

    dgvQuery.Rows.Add();
    DataGridViewCheckBoxCell chkCell = new DataGridViewCheckBoxCell();
    chkCell.TrueValue = true;
    chkCell.FalseValue = false;
    dgvQuery[0, 1] = chkCell;
    dgvQuery[0, 1].Value = false;
    dgvQuery.Rows[1].HeaderCell.Value = "Show";

    dgvQuery.Rows.Add();

    DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
    btnCell.Value = "Edit";
    dgvQuery[0, 2] = btnCell;
    dgvQuery.CellMouseClick += new DataGridViewCellMouseEventHandler(dgvQuery_CellMouseClick);
    dgvQuery.Rows[2].HeaderCell.Value = "Criteria";

    dgvQuery.Rows.Add();
    dgvQuery.Rows[3].HeaderCell.Value = "Query";
    dgvQuery.Rows[3].ReadOnly = true;
}

C#:
private void dgvQuery_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    switch (e.RowIndex)
    {
        case 1:     //  Show CheckBox
            DataGridView myView = (DataGridView)sender;
            DataGridViewCheckBoxCell myCell = (DataGridViewCheckBoxCell)myView[e.ColumnIndex, e.RowIndex];
            if (Convert.ToBoolean(myCell.Value) == true)
            {
            }
            else
            {
            }

            break;
        case 2:     //  Edit Button
            frmCriteria frmShowCriteria = new frmCriteria();
            DialogResult drButton;
            sQueryField = dgvQuery.Columns[e.ColumnIndex].HeaderCell.Value.ToString();
            if (dgvQuery[e.ColumnIndex, 3].Value != null)
                sSQLQuery = dgvQuery[e.ColumnIndex, 3].Value.ToString();
            else
                sSQLQuery = "";
            drButton = frmShowCriteria.ShowDialog();
            if (drButton == System.Windows.Forms.DialogResult.OK)
            {
                dgvQuery[e.ColumnIndex, 3].Value = sSQLQuery;
            }
            break;
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,451
Location
Sydney, Australia
Programming Experience
10+
The Value property ALWAYS contains the current value of the cell, no matter what type or value that may be. The CellMouseClick event is basically irrelevant for a check box cell. For any cell, if you're interested in knowing what the new value is when it changes, you handle the CellValueChanged event. If you had taken the time to read the documentation for the DataGridViewCheckBoxCell (you should pretty much ALWAYS read the documentation for new types and members, especially if they don't work the way you expect) then you'd know that the CellContentClick event is raised when the user clicks the check box but that just starts an editing session. The Value doesn't change until focus moves on, just like other cells. The documentation explains what to do if you want to force the Value to change on a click.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
Slight aside: It has been years since I've worked with the checkboxes in the DataGridView and having some suppressed PTSD memories does not help. There is also a maddening UI inconsistency with checkboxes in the DataGridView depending on if you are a mouse user or keyboard user. The PTSD is from after about two weeks of battling in our bug database with testers and PMs to implement the features spec'ed out by the PMs, but could not be consistently verified by two independent testers, that we'd finally booked a conference room to try to do live demos of the bugs that were not per spec regarding focus and enable/disable checkboxes. There we finally realized that one tester was always using a mouse, while another was using a keyboard most of the time (because he has an accessibility testing background). To make matters worse trying to use the mouse to set the focus and then the keyboard to enable/disable would also behave slightly differently. In the end, to implement what the PMs wanted required me to roll my own DataGridCheckBoxColumn/Cell implementation. Essentially starting from scratch on unplanned work in a project that was already two weeks behind due to the bugs initially found, and adding the potential for even more new bugs because of a brand new untested code base. Good recipe for PTSD.

Now back on topic: @jmcilhinney has it right on the money regarding reading the documentation. The DataGridView documentation is actually very well written, as are the documents on how to extend it. I don't know if they are still available, but if you can find them, they are gold: articles, posts, and demos by Regis Bird with regards to the DataGridView. A lot of design philosophy is embedded in them and it adds nuance to the documentation to understand why things work the way they do.
 

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
58
Programming Experience
10+
Thanks for the comments and direction.
Documentation is great for DataGridViewCheckBoxCell and DataGridViewButtonCell. I ended up using CurrentCellDirtyStateChanged and calling CommitEdit which fired CellValueChanged.
 
Last edited:
Top Bottom