开发者

Datagridview combobox business object updating reference

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


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 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜