0

I have a Winforms DataGridView in my application.

I've two checkbox columns along with 5 other columns from the database. These two checkbox columns are added usng DataGridViewCheckBoxColumn.

When the user clicks on the 2nd checkbox, I need to show a message to the user if the first checkbox is not checked for that row.

How do I go about this? I tried this,but the cell value is coming as null. What am i doing wrong?

private void dgTest_CellClick(System.Object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCheckBoxCell officialCbCell = row.Cells[1] as DataGridViewCheckBoxCell;
    DataGridViewCheckBoxCell includeCbCell = row.Cells[0] as DataGridViewCheckBoxCell;

    if (officialCbCell != null)
    {
        if (officialCbCell.Value != null && (bool)officialCbCell.Value == true)
        {
            if (includeCbCell != null && (bool)includeCbCell.Value == false)
            {
                MessageBox.Show("INVALID");
            }
        }
    }
}

Thanks.

1
  • Any inputs on this please?Thanks. Commented Mar 10, 2011 at 12:51

2 Answers 2

7

You can try using the CellValueChanged event of the grid

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        bool isChecked = (Boolean) dataGridView1[0, e.RowIndex].FormattedValue;

        if (isChecked)
            dataGridView1[1, e.RowIndex].Value = true;
    }
}

if checked then you can set the other column as also checked or any other validation

Sign up to request clarification or add additional context in comments.

Comments

1

CellContentClick event and cell.EditingCellFormattedValue property are also useful if you just un/clicked the cell.

Comments

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.