2

I want to get a checkbox value from a datagridview(True/False) but I always get a value "null", here is the code where i get the value of the checkbox:

DataGridViewCheckBoxCell boolean = (DataGridViewCheckBoxCell)dgv[e.ColumnIndex, e.RowIndex];
string checkCheckboxChecked = ((bool)boolean.FormattedValue) ? "False" : "True";

This code returns a false in the Boolean.FormattedValue even the checkbox is checked and also I tried another:

object value = dgvVisual[e.ColumnIndex, e.RowIndex].Value;

And this code return a value of null

Why does this happen?

P.S. e is an event of the CELL CONTENT CLICK.

Here is the full code of the datagridview cell content click:

private void dgvVisual_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int Number1= int.Parse(dgvVisual[0, e.RowIndex].Value.ToString());
    int Number2 = (e.ColumnIndex - 1);                
    DataGridViewCheckBoxCell boolean = (DataGridViewCheckBoxCell)dgvVisual[e.ColumnIndex, e.RowIndex];
    bool checkCheckboxChecked = (null != boolean && null != boolean.Value && true == (bool)boolean.Value);
    //string checkCheckboxChecked = "";
    if (checkCheckboxChecked)
    {
        //do something if the checkbox is checked
    }
    else
    {
        //do something if the checkbox isn't
    }
}

SOLVED: I changed the CELL END EDIT EVENT and add the click content to datagridview.CurrentCell to another cell.

8
  • Try dgvVisual[e.ColumnIndex, e.RowIndex].Value.toString(); Commented Jul 21, 2012 at 2:13
  • NULL Reference exception if i use dgvVisual[e.ColumnIndex, e.RowIndex].Value.toString(); Commented Jul 21, 2012 at 2:16
  • Since you are using an eventargs which DataGridView event is this code in? Commented Jul 21, 2012 at 2:23
  • e is an event of DatagridView CELL CONTENT CLICK Commented Jul 21, 2012 at 2:24
  • use the dgv_CellEndEdit event instead Commented Jul 21, 2012 at 2:32

1 Answer 1

1

Its a bit odd calling the cell boolean. And then using its FormattedValue property. I added a DataGridView to a Form, added two columns Text and Checkbox. CheckBox is a DataGridViewCheckBoxColumn. Then I added a button and this should give you the idea:

private void button1_Click(object sender, EventArgs e)
{
    dgv.AutoGenerateColumns = false;
    DataTable dt = new DataTable();
    dt.Columns.Add("Text");
    dt.Columns.Add("CheckBox");
    for (int i = 0; i < 3; i++)
    {
        DataRow dr = dt.NewRow();
        dr[0] = i.ToString();
        dt.Rows.Add(dr);
    }
    dgv.DataSource = dt;            
}

private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridViewRow row in dgv.Rows)
    {
        var oCell = row.Cells[1] as DataGridViewCheckBoxCell;
        bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

is OCELL a DataGridViewCheckBoxCell?
This doesn't work also, It returns a "FALSE" value even the checkbox is checked
It doesn't work for me also...is it that i blind the data from the database and i changed the Column's data property name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.