How to check if dataGridView checkBox is checked?

Ruslan picture Ruslan · Dec 8, 2013 · Viewed 74.4k times · Source

I'm new to programming and C# language. I got stuck, please help. So I have written this code (c# Visual Studio 2012):

private void button2_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
         if (row.Cells[1].Value == true)
         {
              // what I want to do
         }
    }
}

And so I get the following error:

Operator '==' cannot be applied to operands of type 'object' and 'bool'.

Answer

İlker Elçora picture İlker Elçora · Mar 25, 2014

You should use Convert.ToBoolean() to check if dataGridView checkBox is checked.

private void button2_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
         if (Convert.ToBoolean(row.Cells[1].Value))
         {
              // what you want to do
         }
    }
}