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'.
You should use Convert. ToBoolean() to check if dataGridView checkBox is checked.
'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.
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.
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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With