我有一个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。
PIPIONE
拉莫斯之舞
相关分类