猿问

DataGridView 中的复选框值始终为 true

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


我已经尝试过 woth 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上搜索了这么多帖子,甚至试图使用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;

            }

        }

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


青春有我
浏览 108回答 2
2回答

PIPIONE

您需要找到控件并将其强制转换为复选框对象,然后查看它是否处于选中状态。该属性是复选框存储真/假值的位置,用于检查这些值。现在,您只是在检查单元格中的数据,以查看它是否有任何值,并且每次都返回true。我在下面这样做的方式是如何使用 ASP.NET Webforms GridView对象来执行此操作。我认为这与Windows Forms的想法大致相同,只是DataGridView可能有不同的方法来“查找你的控件”,而不是你在GridView的 ASP.NET Webforms中使用的方法。但我敢打赌,情况可能也是一样的。CheckedFindControl我四处走动,无法使用Windows Forms测试此确切功能,但是,但是,为了使逻辑正常工作而需要做的事情背后的想法是完全相同的。将您的状况更改为如下所示:foreach (DataGridViewRow row in computerSelection.compGridView.Rows)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Don't cast as bool. Convert to Checkbox object and see if it is checked.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (((CheckBox)row.FindControl("CheckBox")).Checked) // row.FindControl might be different but might be the same for Winforms.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

拉莫斯之舞

我找到了做到这一点的方法!使用 CellContentClick Event,然后使用 EditedFormatedValue 属性,这次布尔值为 REAL。&nbsp;private void compGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.ColumnIndex == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((bool)compGridView.Rows[e.RowIndex].Cells[0].EditedFormattedValue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComputersList.Add(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComputersList.Remove(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }谢谢编辑:弄清楚为什么我一开始就有这个问题,这甚至有点滑稽。我有一个事件,当关闭表单时,每个复选框的值都会变为TRUE!&nbsp;private void ComputerSelection_FormClosed(object sender, FormClosedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < compGridView.Rows.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; compGridView.Rows[i].Cells[0].Value = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答