【发布时间】:2021-01-12 14:39:52
【问题描述】:
当我在 DataGridView 中循环遍历每一行时,不确定为什么设置行的 ReadOnly 属性不起作用(我仍然可以编辑所有行):
foreach (DataGridViewRow row in dataGridEthnicityData.Rows)
{
string EthnicityProfilingCompanyName = row.Cells["EthnicityProfilingCompanyName"].Value.ToString();
string EthnicityProfilingCompanyID = row.Cells["EthnicityProfilingCompanyID"].Value.ToString();
if (EthnicityProfilingCompanyName != ProfilingEntityName && EthnicityProfilingCompanyID != ProfilingEntityID)
row.ReadOnly = true;
else row.ReadOnly = false;
}
如果有人能指出我正确的方向,不胜感激。我必须改变循环方式吗?我正在考虑使用带计数器的循环,以便将其用作行索引。
谢谢。
【问题讨论】:
-
它不起作用怎么办?您点击了
row.ReadOnly = true行,但之后您仍然可以更改该行吗?你确定你没有落入else吗? -
@BrootsWaymb 我仍然可以编辑我设置为 row.ReadOnly = true 的行。我调试并确保它通过了第一个 If 条件。
-
@KateB 对于此类问题,您应该分享minimal reproducible example,我们无法判断标准或代码的其他相关部分可能有什么问题。但在这里,我尝试用干净的最小代码分享答案。
-
@RezaAghaei 我很抱歉,发现该行被禁用,除了数据网格中的组合框和按钮。我的数据网格绑定到数据表,并且组合框也绑定了。
-
正如答案中已经提到的,将您的
foreach循环移动到DataBindingComple事件或作为更好的选择使用CellBeginEdit而不使用foreach循环。虽然 ComboBos 样式类似于下拉菜单,但它是只读的,并且无法编辑单元格。
标签: c# .net winforms datagridview