【问题标题】:CheckBox value in DataGridView is always trueDataGridView 中的 CheckBox 值始终为 true
【发布时间】:2019-01-31 15:47:48
【问题描述】:

我有一个 WinForm 应用程序,它具有 DataGridView,其中一列带有 CheckBox。 用复选框选择几行后,我需要迭代行并检查 CheckBox 是否被勾选。

我已经尝试过 for 和 foreach 循环,但每次 bx 的值都是真的! 有趣的是,我有一个选择全部的按钮和一个使用代码清除所有的按钮,而且它有效!

我创建 DataGridView 的代码:

 ComputerSelection computerSelection = new ComputerSelection();
            DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn();
            checkBox.ValueType = typeof(bool);
            checkBox.Name = "CheckBox";
            checkBox.HeaderText = "Select";
            computerSelection.compGridView.Columns.Add(checkBox);
            computerSelection.compGridView.Columns.Add("Name", "Name");
            computerSelection.compGridView.Columns.Add("AgentVersion", "Agent Version");
            computerSelection.compGridView.Columns.Add("Status", "Status");
            computerSelection.compGridView.Columns.Add("Domain", "Domain");

迭代代码(我搜索的大多数帖子都共享该解决方案作为正确的解决方案):

 foreach (DataGridViewRow row in computerSelection.compGridView.Rows)
            {
                if ((bool)row.Cells["CheckBox"].Value)
                {
                    computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());
                }
            }

即使在未选中的复选框上,此代码也始终返回 TRUE。我在 StackOverFlow 上搜索了很多帖子,甚至尝试使用 as DataGridViewCheckBoxCell 但没有成功。

选择所有按钮代码(使用相同的机制:():

 private void SelectAllButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < compGridView.Rows.Count; i++)
            {
                compGridView.Rows[i].Cells[0].Value = true;
            }
        }

我需要在每次迭代后“row.Cells[1].Value.ToString()”代码将返回 false 或 true,而不总是 true。

【问题讨论】:

  • 尝试通过索引 0 访问它,就像您在 SelectAll 函数中所做的那样,看看您是否得到不同的行为。设置断点以在强制转换之前查看值是什么。
  • @user7733611 已经尝试使用索引 0,但没有成功。还检查了您关于在转换之前检查值的建议,它仍然与转换后的值相同:(

标签: c#


【解决方案1】:

您需要找到您的控件并将其转换为复选框对象并查看它是否被选中。 Checked 属性是复选框存储是否选中的真/假值的地方。现在你只是在检查一个单元格的数据,看看它是否有任何值,并且每次都返回 true。我在下面执行的方式是您将如何使用 ASP.NET Webforms GridView 对象执行此操作。我认为这与 Windows 窗体的想法基本相同,只是 DataGridView 可能有一种不同的方法来“找到你的控件”,而不是你在 ASP.NET Webforms 中为 GridView 使用的FindControl 方法。但我敢打赌它可能是一样的。

我出去玩了,不能用 Windows 窗体测试这个确切的功能,但是你需要做什么来使你的逻辑工作背后的想法是完全相同的。

把你的条件改成这样:

foreach (DataGridViewRow row in computerSelection.compGridView.Rows)
        {
            // Don't cast as bool. Convert to Checkbox object and see if it is checked. 
            if (((CheckBox)row.FindControl("CheckBox")).Checked) // row.FindControl might be different but might be the same for Winforms.
            {
                computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());
            }
        }

【讨论】:

    【解决方案2】:

    我找到了方法! 使用 CellContentClick 事件,然后使用 EditedFormattedValue 属性,这次 bool 值为 REAL。

     private void compGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    if ((bool)compGridView.Rows[e.RowIndex].Cells[0].EditedFormattedValue)
                    {
                        ComputersList.Add(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
                    }
                    else
                    {
                        ComputersList.Remove(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
                    }
                }
            }
    

    谢谢

    编辑: 弄清楚为什么我首先遇到了这个问题,这甚至有点有趣。我有一个事件,当关闭表单时,每个复选框的值都变为 TRUE!

     private void ComputerSelection_FormClosed(object sender, FormClosedEventArgs e)
            {
                for(int i = 0; i < compGridView.Rows.Count; i++)
                {
                    compGridView.Rows[i].Cells[0].Value = true;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      相关资源
      最近更新 更多