18

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

Pascalz
  • 2,348
  • 1
  • 24
  • 25
Ruslan
  • 351
  • 1
  • 4
  • 13

6 Answers6

40

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
         }
    }
}
İlker Elçora
  • 610
  • 5
  • 13
  • This is by far a better method than the accepted answer... `Convert.ToBoolean()` does not require a `null` value check hence streamlining the code too. – Brendan Gooden Aug 11 '16 at 01:13
6

All of the answers on here are prone to error,

So to clear things up for people who stumble across this question,

The best way to achieve what the OP wants is with the following code:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell; 

    //We don't want a null exception!
    if (cell.Value != null)
    {
        if (cell.Value == cell.TrueValue)
        {
           //It's checked!
        }  
    }              
}
Alex
  • 1,208
  • 16
  • 26
4

Value return an object type and that cannot be compared to a boolean value. You can cast the value to bool

if ((bool)row.Cells[1].Value == true)
{
    // what I want to do
}
  • what does .Cells[1] number represent? – Ruslan Dec 10 '13 at 13:02
  • @RuslanVasiljev Hopefully the value of a cell that is of type of Checkbox. (You can set a column in the datagrid to be of this type and then check if it is checked or not) – KansaiRobot Sep 12 '18 at 01:52
3
if (Convert.ToBoolean(row.Cells[1].EditedFormattedValue))
{
    //Is Checked
}
  • **From review queue:** May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. See also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers). – help-info.de Jul 19 '17 at 05:40
  • Please have a look at [how to answer](https://stackoverflow.com/help/how-to-answer) and update your answer to provide more detail. Specifically, it would be helpful if you explained how this solves the problem – Ortund Jul 19 '17 at 07:18
0

Slight modification should work

if (row.Cells[1].Value == (row.Cells[1].Value=true))
{
    // what I want to do
}
myteardrop4u
  • 31
  • 1
  • 1
  • 8
0

This code above was wrong!

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell; 

    // Note: Can't check cell.value for null if Cell is null 
    // just check cell != null first
    //We don't want a null exception!
    if (cell.Value != null)
    {
        if (cell.Value == cell.TrueValue)
        {
           //It's checked!
        }  
    }              
}
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111