【问题标题】:How to check if dataGridView checkBox is checked?如何检查是否选中了 dataGridView 复选框?
【发布时间】:2013-12-25 12:18:32
【问题描述】:

我是编程和 C# 语言的新手。我被卡住了,请帮忙。所以我写了这段代码(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
         }
    }
}

所以我得到以下错误:

运算符“==”不能应用于“object”和“bool”类型的操作数。

【问题讨论】:

    标签: c# visual-studio-2012 datagridviewcheckboxcell


    【解决方案1】:

    Value 返回一个对象类型,不能与布尔值进行比较。您可以将值转换为 bool

    if ((bool)row.Cells[1].Value == true)
    {
        // what I want to do
    }
    

    【讨论】:

    • .Cells[1] 数字代表什么?
    • @RuslanVasiljev 希望是复选框类型的单元格的值。 (可以在datagrid中设置一列为该类型,然后检查是否勾选)
    【解决方案2】:

    您应该使用Convert.ToBoolean() 来检查是否选中了dataGridView checkBox。

    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
             }
        }
    }
    

    【讨论】:

    • 这是迄今为止比公认答案更好的方法...Convert.ToBoolean() 不需要检查 null 值,因此也简化了代码。
    【解决方案3】:

    这里所有的答案都容易出错,

    所以为了让那些偶然发现这个问题的人弄清楚,

    实现 OP 想要的最好方法是使用以下代码:

    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!
            }  
        }              
    }
    

    【讨论】:

      【解决方案4】:

      稍微修改一下就可以了

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

      【讨论】:

        【解决方案5】:
        if (Convert.ToBoolean(row.Cells[1].EditedFormattedValue))
        {
            //Is Checked
        }
        

        【讨论】:

        • 来自审核队列:我可以请求您在您的答案周围添加更多上下文。仅代码的答案很难理解。如果您可以在帖子中添加更多信息,它将帮助提问者和未来的读者。另请参阅Explaining entirely code-based answers
        • 请查看how to answer 并更新您的答案以提供更多详细信息。具体来说,如果您解释一下这是如何解决问题的,将会很有帮助
        【解决方案6】:

        上面的代码是错误的!

        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!
                }  
            }              
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-14
          • 1970-01-01
          • 2019-11-19
          • 1970-01-01
          相关资源
          最近更新 更多