Datagridview组合框业务对象更新参考

发布于 2024-09-25 05:53:37 字数 584 浏览 6 评论 0原文

我最近在此处提出了这个问题并得到了答案。不过,我现在尝试在绑定到 BindingList< 的 DataGridView 上应用相同的逻辑。 T>课程对象。 Curriculum 类具有 Year 类型的属性。我正在尝试使用 ComboBoxColumn 来更新课程对象多年来的引用。

组合框列绑定到 BindingList< T>多年来,如果我设置显示成员或值成员,就会出错,因此我将它们保留为空。执行此操作后,datagridview 成功加载并正确显示数据(我覆盖了年份类上的 ToString 方法)。但是,如果我从组合框中选择另一个年份对象,一旦编辑结束,它就会抛出异常,指出它无法将字符串转换为年份类型。

看起来我需要一个 TypeConverter 来做到这一点,但问题是组合框正在显示一个描述性值,我不能保证该值对于该年份对象是唯一的 - 所以我无法从给定的年份对象中获取年份对象细绳。

有没有人有过类似情况的经验,这一定是一件很常见的事情,但谷歌这次让我失望了。

马龙

I recently asked this question on here and got the answer. However I'm now trying to apply the same logic on a DataGridView which is bound to a BindingList< T > of Curriculum objects. The Curriculum class has a property of type Year. I'm trying to use a ComboBoxColumn to update the reference the curriculum object has of years.

The comboboxcolumn is bound to a BindingList< T > of years, it errors if I set either the display member or the value member so I left them null. Doing this the datagridview successfully loads and displays the data correctly (I overrode the ToString method on the year class). However, if I choose another year object from the combobox, as soon as it end edits it throws and exception saying it can't convert string to type year.

It looks like I need a TypeConverter to do it, but the problem is the combobox is displaying a descriptive value, which I can't guarantee will be unique to that year object - so I have no way of getting a year object from a given string.

Has anyone got any experience in situations like these, it must be a pretty common thing to want to do but google has failed me on this occasion.

Marlon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-10-02 05:53:37

此处相同的问题。似乎组合框列中的对象绑定无法正常工作,您必须指定 ValueMember。

对于我正在从事的特定项目,我得出的结论是不值得实现自定义类型描述符,因此,我使用了一个相当可怕的黑客:

在我绑定的实体中,我有以下内容:

class TestEntity
{
    public TestEntity BindingHack_ValueMember
    {
        get
        {
           return this;
        }
    }
    public string BindingHack_DisplayMember
    {
        get
        {
            return this.ToString();
        }
    }
}

该列的数据绑定看起来像这样:

column.DataPropertyName = "Foo";
column.DisplayMember = "BindingHack_DisplayMember";
column.ValueMember = "BindingHack_ValueMember";

也许有点难看,但它有效......

Same problem as here. Seems that object binding in a combobox column doesn't work properly and you have to specify a ValueMember.

For the particular project I am working on, I came to the conculsion that it was not worth implementing a custom type descriptor, so instead, I am using a fairly horrible hack:

In the entity that I am binding to, I have the following:

class TestEntity
{
    public TestEntity BindingHack_ValueMember
    {
        get
        {
           return this;
        }
    }
    public string BindingHack_DisplayMember
    {
        get
        {
            return this.ToString();
        }
    }
}

And the databinding for the column looks like this:

column.DataPropertyName = "Foo";
column.DisplayMember = "BindingHack_DisplayMember";
column.ValueMember = "BindingHack_ValueMember";

A little ugly, perhaps, but it works ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文