英文:
How to make ONE cell ReadOnly?
问题
只有一个单元格,不是整行,也不是整列。DataGridView
的 ReadOnly 属性设置为 false(默认)。如果在行级别满足条件,允许特定单元格的 ReadOnly 设置为 false/true。
foreach (DataGridViewRow row in dgv.Rows)
{
if (!Convert.ToBoolean(row.Cells["Cell 0"].Value))
{
row.Cells["Cell 3"].ReadOnly = false; // 当前行的 Cell 3(仅此行,仅此单元格)设置为 ReadOnly = false
}
else
{
row.Cells["Cell 3"].ReadOnly = true; // 当前行的 Cell 3(仅此行,仅此单元格)设置为 ReadOnly = true
}
}
英文:
Not the whole row, not the whole column, just one cell. The DataGridView
ReadOnly property is set to false (default). If the condition is met at the row level, allow specific cells to be ReadOnly false/true
foreach (DataGridViewRow row in dgv.Rows)
{
if (!Convert.ToBoolean(row.Cells["Cell 0"].Value))
{
row.Cells["Cell 3"].ReadOnly = false; // The Cell 3 for the current row (only this row, only this cell)
// is set to ReadOnly = false
}
else
{
row.Cells["Cell 3"].ReadOnly = true; // The Cell 3 for the current row (only this row, only this cell)
// is set to ReadOnly = true
}
}
答案1
得分: 1
在row.Cells[]
中提供的标识符是不正确的。你应该在那里提供列名,而不是单元格名称!
示例:
dataGridView1.Rows.Add("test", "false");
dataGridView1.Rows.Add("sfsdf", "true");
dataGridView1.Rows.Add("hjdfg", "false");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
// 将检查当前行和列“Column2”中单元格的值
if (Convert.ToBoolean(row.Cells["Column2"].Value))
row.Cells["Column2"].ReadOnly = true; // 设置该单元格的属性
}
如果你希望缩短该代码以将特定列中的所有单元格的“只读”属性设置为特定单元格值,你可以这样做:
row.Cells["Column2"].ReadOnly = Convert.ToBoolean(row.Cells["Column2"].Value);
英文:
The identifiers you're providing in row.Cells[]
is incorrect. You should be providing the column name there and not cell names!
Example:
dataGridView1.Rows.Add("test", "false");
dataGridView1.Rows.Add("sfsdf", "true");
dataGridView1.Rows.Add("hjdfg", "false");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
// Will check the value of the cell in current row and column "Column2"
if (Convert.ToBoolean(row.Cells["Column2"].Value))
row.Cells["Column2"].ReadOnly = true; // Sets the property of that cell
}
If you want you can shorten that code to set the readonly
property all cells in a specific column to the specific cell value:
row.Cells["Column2"].ReadOnly = Convert.ToBoolean(row.Cells["Column2"].Value);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论