1
Reply

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

David Davies

David Davies

Feb 10 2006 4:34 PM
2.1k

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: 

 

 

//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


Answers (1)