2

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

Community
  • 1
  • 1
Marlon
  • 2,129
  • 3
  • 21
  • 40

1 Answers1

3

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

Community
  • 1
  • 1
Laviak
  • 406
  • 1
  • 5
  • 8
  • Thanks Laviak, I'd resorted back to using idents but if the situation arises again I shall do it this way. – Marlon Jan 14 '11 at 14:57