C# 如何根据其他单元格中的值禁用(设为只读)DataGridView CheckBox 列中的单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18616701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable (make read only) a cell in a DataGridView CheckBox column based on the value in other cells?
提问by Jake
I found many similar questions and answers, but none helps me to solve my issue.
我发现了许多类似的问题和答案,但没有一个能帮助我解决我的问题。
Please find my DataGridView
below
请在DataGridView
下面找到我的
What I want to do is to disable the check box if the namecell is empty at run time.
如果名称单元格在运行时为空,我想要做的是禁用复选框。
I tried many methods, but all the time the cell is disabled (read only) after I checked it.
我尝试了很多方法,但在我检查后,单元格一直被禁用(只读)。
I tried something like this:
我试过这样的事情:
private void sendDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (sendDGV.CurrentRow.Cells[1].Value != null)
{
sendDGV.CurrentRow.Cells[2].ReadOnly = false;
sendDGV.Update();
}
else
{
sendDGV.CurrentRow.Cells[2].ReadOnly = true;
sendDGV.Update();
}
}
采纳答案by Chris
To handle the changes in column name, you can use the DataGridView.CellValueChanged
event. The e
parameter give you access to:
要处理列名称的更改,您可以使用该DataGridView.CellValueChanged
事件。该e
参数使您可以访问:
columnIndex
property, so you can test if the change is made on the namecolumn (index 1).rowIndex
property, so you retrieve the concerned row and change the values you want.
columnIndex
属性,因此您可以测试是否对名称列(索引 1)进行了更改。rowIndex
属性,因此您可以检索相关行并更改所需的值。
private void DataGridView1_CellValueChanged(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
//second column
if (e.ColumnIndex == 1) {
object value = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (value != null && value.ToString() != string.Empty) {
DataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = false;
} else {
DataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
}
}
}
EDIT
编辑
As someone else noted, in order to have the checkbox
disabled for new added rows (especially if the AllowUserToAddRow
property is set to true
), you can handle the RowAdded
event:
正如其他人所指出的,为了checkbox
禁用新添加的行(特别是如果该AllowUserToAddRow
属性设置为true
),您可以处理该RowAdded
事件:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
}
回答by Flavia Obreja
You can use DataGridView.CellValueChanged event:
您可以使用 DataGridView.CellValueChanged 事件:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
if (e.ColumnIndex == 1 && dataGridView1[1, e.RowIndex].Value.ToString() != "")
dataGridView1[2, e.RowIndex].ReadOnly = false;
else
dataGridView1[2, e.RowIndex].ReadOnly = true;
}
}
But in order to have the checkbox disbled at first, make sure you set the column to be ReadOnly using the designer and also, in the DataGridView.RowsAdded event, set the checkbox property ReadOnly = true for the new created row:
但是为了首先禁用复选框,请确保使用设计器将列设置为只读,并且在 DataGridView.RowsAdded 事件中,为新创建的行设置复选框属性 ReadOnly = true:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
dataGridView1[2, e.RowIndex].ReadOnly = true;
}
回答by Mohamed Yunus
Simple, there is inbuilt read onlyproperty in Visual Studio, mark it as true
很简单,Visual Studio有内置的只读属性,标记为 true
回答by Vojta
Quite old thread but I think you can use CellBeginEditevent and Cancel the event on your condition. It is not disable the column it is rather cancel editing the desired column value.
很旧的线程,但我认为您可以使用CellBeginEdit事件并根据您的条件取消该事件。它不是禁用列,而是取消编辑所需的列值。
1) subscribe for the event:
1) 订阅事件:
this.dataGridView1.CellBeginEdit += DataGridView1OnCellBeginEdit;
2) event handler:
2) 事件处理程序:
private void DataGridView1OnCellBeginEdit(object sender, DataGridViewCellCancelEventArgs args)
{
var isTicked = this.dataGridView1.Rows[args.RowIndex].Cells[args.ColumnIndex].Value;
args.Cancel = (isTicked is bool) && ((bool)isTicked);
}
I have used the event to get one inclusive checkbox.
我已经使用该事件获得了一个包容性复选框。
It means only one out of the three columns("None", "Read", "Full") can be "true"
这意味着三列中只有一列(“无”,“读取”,“完整”)可以是“真”