Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through datagridview and reading the CheckBoxColumn value

I have a datagridview with one DataGridViewCheckBoxColumn and some other TextBox Columns. I want to loop through each cell and see if the checkbox is checked then do something. I am using the following looping method. Is there a better way to do??

I have used or condition because in some computers it brings .Value as Checked and in some it bring .Value as true.

     foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if ((bool)(row.Cells["Checkbox"]).Value || (CheckState)row.Cells["Checkbox"].Value == CheckState.Checked)
                    {   
                        // Do something
                    }

                }

Thanks in advance.

like image 967
Marshal Avatar asked Oct 16 '25 23:10

Marshal


1 Answers

i think this will be faster than foreach

 for (int i = 0; i < dataGridView.Rows.Count -1; i++)
 {
    DataGridViewRow row = dataGridView.Rows[i];
    if ((bool)(row.Cells["Checkbox"]).Value 
        || (CheckState)row.Cells["Checkbox"].Value == CheckState.Checked)
    {
         // Do something
    }
 }
like image 193
Nighil Avatar answered Oct 19 '25 12:10

Nighil