C# 无法在 DataGridViewCheckBoxColumn 中选中复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11269181/
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
Cannot check box in DataGridViewCheckBoxColumn?
提问by Ricardo Altamirano
I'm creating a simple DataGridViewwith a check box column and a text column (more columns will follow, but this is the minimal working example that I'm trying to get working). When I run this code, the checkbox columns appears, but I can't check the boxes.
我正在创建一个简单DataGridView的复选框列和一个文本列(更多的列将跟随,但这是我试图开始工作的最小工作示例)。当我运行此代码时,复选框列出现,但我无法选中这些框。
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.ThreeState = false;
checkColumn.Width = 20;
MyDataGridView.Columns.Add(checkColumn);
MyDataGridView.Columns.Add(new DataGridViewTextBoxColumn());
Since nothing appears in this case, I thought to add some dummy data.
由于在这种情况下什么都没有出现,我想添加一些虚拟数据。
for (int i = 0; i < 10; i++)
{
MyDataGridView.Rows.Add(new Object[] { true, "test"});
}
Normally, the DataGridViewis populated with data bound from a list of custom objects, like in this question of mine, but I thought it would be better to get this working in a basic way before moving on.
通常,DataGridView填充了从自定义对象列表绑定的数据,就像在我的这个问题中一样,但我认为在继续之前最好以基本方式使其工作。
I'm not trying to set the checked state programmatically, but rather let the user select and then use that selection in various other event handlers.
我不是试图以编程方式设置选中状态,而是让用户选择然后在各种其他事件处理程序中使用该选择。
采纳答案by Luis Quijada
The code seems to be fine, so I just can tell you to check and ensure that the following DataGridViewproperties are properly set: ReadOnlyset to Falseand Enabledset to True.
代码似乎没问题,所以我只能告诉您检查并确保DataGridView正确设置了以下属性:ReadOnlyset toFalse和Enabledset to True。
回答by Mike de Klerk
This can happen as well when you populate the DataGridViewwith an object, that has public Boolean property(the CheckBoxin the DataGridView) that has a private setter. A column in the DataGridViewthat represents this (read-only) property is automatically read-only as its not allowed to set the property externally (=out side the code of the object).
当您DataGridView使用具有public Boolean property(CheckBox在 DataGridView 中的)具有private setter的对象填充 时,也会发生这种情况。在A柱DataGridView表示此(只读)属性是自动只读作为其不允许外部设定的属性(=出侧的对象的代码)。
public class ExampleObject
{
/// <summary>
/// A public property that can only be read.
/// When a DataGridViewRow is populated with this object, the column representing this Boolean property is automatically read-only.
/// </summary>
public Boolean QCPassed
{
get;
private set;
}
}
回答by Abdul Rehman
Just change the readonly property of DataGridView
只需更改 DataGridView 的 readonly 属性
MyDataGridView.ReadOnly = false;
回答by Abdul Rehman
I had also the same issue with different situations My DataGridView was bound to a DataTable which was filled by a SqlDataReader (which is read-only). I replaced SqlDataReader with SqlDataAdapter works fine.
我在不同情况下也遇到了同样的问题我的 DataGridView 绑定到一个由 SqlDataReader(只读)填充的 DataTable。我用 SqlDataAdapter 替换了 SqlDataReader 工作正常。
DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter("Select <column_names> from <table_name>",con);
da.Fill(dt);
回答by Harry
The table itself may be set to Read-Only even though the checkbox column isn't the table setting will override the column setting.
即使复选框列不是表设置将覆盖列设置,表本身也可能设置为只读。
回答by Heider Sati
I had the same problem, the solution for me was to change the
我遇到了同样的问题,我的解决方案是更改
"EditMode" from "EditProgramatically" into the default of "EditOnKeystrokeOrF2",
“EditMode”从“EditProgramatically”变成默认的“EditOnKeystrokeOrF2”,
this solved my issue.
这解决了我的问题。
All the above suggestions were already implemented.
以上建议均已实施。
Kind Regards Heider
亲切的问候海德

