[英]Making a DataGridView column editable after binding it to a BindingList<T>
I have a DataGridView
and I'm binding it to a BindingList<KeyValuePair<string, float>>
. 我有一个
DataGridView
并将其绑定到BindingList<KeyValuePair<string, float>>
。 Here's the relevant part of the code: 这是代码的相关部分:
dgv.AutoGenerateColumns = false;
DataGridViewTextBoxColumn firstColumn = new DataGridViewTextBoxColumn();
firstColumn.DataPropertyName = "Key";
firstColumn.HeaderText = "First Column";
DataGridViewTextBoxColumn secondColumn = new DataGridViewTextBoxColumn();
secondColumn.DataPropertyName = "Value";
secondColumn.HeaderText = "Second Column";
secondColumn.ReadOnly = false;
secondColumn.ValueType = typeof(float);
dgv.Columns.Add(firstColumn);
dgv.Columns.Add(secondColumn);
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv.MultiSelect = false;
dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
dgv.ReadOnly = false;
_bindingList = new BindingList<KeyValuePair<string, float>>(_someList);
dgv.DataSource = _bindingList;
But the second column is still not editable. 但第二列仍然不可编辑。 What should I do to make the second column editable and the first one not?
我该怎么做才能使第二列可编辑,而第一列则不可编辑?
Edit: I'd like the changes to be reflected on the BindingList
instance itself. 编辑:我希望更改反映在
BindingList
实例本身上。
Edit 2: I have added this line in the end of the code and now I'm getting an error: 编辑2:我在代码的末尾添加了这一行,现在我收到一个错误:
dgv.Columns[1].ReadOnly = false;
I get this error: 我收到此错误:
DataGridView column bound to a read-only field must have ReadOnly set to True.
Edit 3: The problem seems to be that I'm using KeyValuePair
s in my list. 编辑3:问题似乎是我在我的列表中使用
KeyValuePair
。
The problem turns out to be that the Key
and Value
properties of the KeyValuePair
class are read-only. 问题是
KeyValuePair
类的Key
和Value
属性是只读的。 The solution is to create a new class that has public setter
s to represent the data. 解决方案是创建一个具有
public setter
以表示数据的新类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.