T>其绑定到的BindingList℃后,制作一个DataGridView列编辑; [英] Making a DataGridView column editable after binding it to a BindingList<T>

查看:457
本文介绍了T>其绑定到的BindingList℃后,制作一个DataGridView列编辑;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView的,我将它绑定到一个的BindingList< KeyValuePair<字符串,浮动>> 。这里的code的相关部分:

I have a DataGridView and I'm binding it to a 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?

编辑:我想改变的的BindingList 实例本身中得到体现。

I'd like the changes to be reflected on the BindingList instance itself.

编辑2:我在code末尾加上这一行,现在我得到一个错误:

Edit 2: I have added this line in the end of the code and now I'm getting an error:

        dgv.Columns[1].ReadOnly = false;

我得到这个错误:

I get this error:

DataGridView的列必须有只读设置为True。

修改3:这个问题似乎是,我使用 KeyValuePair <​​/ code>在我的列表

Edit 3: The problem seems to be that I'm using KeyValuePairs in my list.

推荐答案

这个问题原来是该 KeyValuePair <​​/ code>类的属性是只读的。该解决方案是创建一个具有 public的setter s到重新present一类新的数据。

The problem turns out to be that the Key and Value properties of the KeyValuePair class are read-only. The solution is to create a new class that has public setters to represent the data.

这篇关于T&gt;其绑定到的BindingList℃后,制作一个DataGridView列编辑;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆