Skip to content

DataGridView - How Do I get the state of a checkbox?

David Davies

Asked Feb 10, 2006, 11:34 AM EST1 answer
Share
LinkedInX
Hi,

I'm new to the DataGridView control and it is really nice, however, it can be difficult to use.

3 of my colomns is a of Type DataGridViewCheckBoxColumn.

It looks good on in the datagridview but how do I get the state of the check box?

I can currently get it using the following code: 

 

 

TypeScript
//Following function fires when any cell is clicked within the DataGridView
//I’m only concerned with Column 1, 2 or 3 as they are the CheckBox Columns
//Each click on the Check Column will change its state – as a String
private void dvSelected_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1 | e.ColumnIndex == 2 | e.ColumnIndex == 3)
            {
                if(dvSelected.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
                {
                    dvSelected.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "False";
                }
                else
                {
                  dvSelected.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "True";
                }
            }

        }

//To Test a specific Cell to see if it is checked

private void button1_Click_1(object sender, EventArgs e)
        {
            if (dvSelected.Rows[0].Cells[1].Value.ToString () == "True") // Test a Check Box cell whithin the data grid view
            {
                MessageBox.Show("Cell is Checked");
            }
            else
            {
                MessageBox.Show("Cell is Un-Checked");
            }

        }
Does anybody know of a different way? Shouldn't I be able to return the cells checkbox propety?

Cheers

David

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Anwar Buchoo
    0
    You can use something like:

    CheckBox selection = (CheckBox)item.FindControl("SelectCheckBox");

    and then check:

    if(selection.Checked)
    {
      // true
    }


    where item is a DataGridItem.


    Rgds.
    0