Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if dataGridView checkBox is checked?

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'.

like image 380
Ruslan Avatar asked Dec 08 '13 11:12

Ruslan


People also ask

How can I check if a CheckBox is checked in DataGridView?

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

How to check if CheckBox is checked in DataGridView Vb net?

'Add a CheckBox Column to the DataGridView at the first position. When the Get Selected button is clicked the following event handler is executed, where a loop is executed over the DataGridView rows. Inside the loop, first a check is performed whether the CheckBox value is TRUE or FALSE i.e. Checked or Unchecked.

How can check CheckBox is checked in Gridview in ASP NET?

With CheckBoxFields and CheckBoxes, you need to get the Checked value to know whether or not it was actually checked. The Text value is actually another property of the CheckBox (see MSDN). You sometimes see this text to the left or right of the CheckBox itself. So what you need to do is first get the CheckBox.


1 Answers

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
         }
    }
}
like image 168
İlker Elçora Avatar answered Oct 19 '22 05:10

İlker Elçora