Hello,

I have created a datagridview with first column is checkbox like below:

dgv.Columns[0].Visible = false;
                    dgv.Columns[1].HeaderText = "Name";
                    if (dgv.Name.Equals("dataGridView1"))
                        dgv.Columns[1].Width = 100;
                    if (dgv.Name.Equals("dataGridView2"))
                        dgv.Columns[1].Width = 145;
                    dgv.Columns[2].HeaderText = "Mobile Number";
                    dgv.Columns[2].Width = 115;
                    dgv.Columns[3].HeaderText = "Date Added";
                    dgv.Columns[3].Width = 95;
                    dgv.Columns[4].Visible = false;
                    dgv.Columns[5].HeaderText = "Group";

                    DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
                    col.FalseValue = "0";
                    col.TrueValue = "1";
                    col.Width = 30;
                    dgv.Columns.Insert(0, col);

But when I tried to get the check state of the checkbox (cell value = ) like below:

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            addOrRemoveCheckedContact(dataGridView2, e);
        }

        private void addOrRemoveCheckedContact(DataGridView dgv, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == 0)
            {
                MessageBox.Show("RowIndex = " + e.RowIndex.ToString() + " , cell value = " + dgv.Rows[e.RowIndex].Cells[0].Value);
                //MessageBox.Show("is check? = " + ((CheckBox)dataGridView1.control.Rows[e.RowIndex].Cells[0]).Checked));
                if (dgv.Rows[e.RowIndex].Cells[0].Value != null)
                {
                    MessageBox.Show("0 = " + dgv.Rows[e.RowIndex].Cells[0].Value.ToString());
                }
                else
                {
                    dgv[0, e.RowIndex].Value = 1;
                    dgv.EndEdit();
                }
                if (dgv.Rows[e.RowIndex].Cells[3].Value != null)
                    MessageBox.Show("1 = " + dgv.Rows[e.RowIndex].Cells[3].Value.ToString());
                if (dgv.Rows[e.RowIndex].Cells[0].Value != null && Convert.ToInt16(dgv.Rows[e.RowIndex].Cells[0].Value) == 0)
                {
                    recipientList.RemoveWhere(delegate(Int64 s) { return s == Convert.ToInt64(dgv.Rows[e.RowIndex].Cells[3].Value); });
                    MessageBox.Show("removed " + dgv.Rows[e.RowIndex].Cells[3].Value.ToString());
                }

            }
        }

the first time i click on the checkbox and I got empty but the 2nd time I clicked on the same checkbox, it displayed "cell value = 1";

Please kindly advice. Thanks in advance!

Cheers,
Mark Thien

Try using the FormattedValue rather than the Value. If you are binding to a database you are likely using a bit column which holds 0 or 1. If the value isnt set it will return null so be sure to check for null values when you check them to avoid nullreference exception :)

EDIT: 0 or false are all the same in code. I forgot to ask, was your problem that it returns 1 instead of true or that it returns empty?

EDIT AGAIN: nevermind, just checked and if you use FormattedValue it will return false for a null value :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.