Wednesday, April 13, 2011

How to bound a DataGridViewComboBoxColumn to a object?

I'm trying to bound a DataGridViewComboBoxColumn to an instance of Foo, but when i set a value on the grid i got a ArgumentException telling me that i can not convert from String to Foo.

var data = (from item in someTable
            select new { Foo = item.foo, Bar = item.Bar }).ToList();
grid.DataSource = data;
column.DataPropertyName = "Foo";
column.DataSource = (from foo in Foo select foo).ToList (); //foo is an instance of Foo
column.DisplayMember = "SomeNameField"; //Foo.SomeNameField contains a description of the instance

Am i missing something? is it possible to databind to a complex object?

UPDATE:

I implemented a TypeConverter and overrided CanConvertFrom, CanConvertTo, ConvertTo, ConvertFrom. Now i'm getting "FormatException: The DataGridViewComboBoxCell value is not valid"

Any ideas?

From stackoverflow
  • You are missing a possible piece.

    column.DataPropertyName = "Foo";
    column.DisplayMember = "SomeNameField"; 
    column.ValueMember = "Bar"; // must do this, empty string causes it to be 
                                // of type string, basically the display value
                                // probably a bug in .NET
    column.DataSource = from foo in Foo select foo;
    grid.DataSource = data;
    

    UPDATE:

    Actually, after reading your question again, I think you are facing that noted bug. There is unfortunately no way to make it return the bound object without using a custom TypeDescriptor/TypeConverter/BindingSource.

    Answer for binding to a complex object. No by default. I wrote quite a nice one for my current project. This involves making a custom TypeDescriptor/TypeConverter/BindingSource that returns all the nested properties. Another 'bug', you cant use '.' for a member separator, I had to resort to ':' instead.

    AlbertEin : How do i make the TypeConverter?
    leppie : You inherit from TypeConverter and override the GetProperties/GetPropertiesSupported methods. Then apply the type to the TypeConverterAttribute on your desired target type. The subject-area is very large. Google for some more direction.
    AlbertEin : I implemented a TypeConverter and overrided CanConvertTo, CanConvertFrom, ConvertTo and ConvertFrom, now i'm getting the error "FormatException: The value of the DataGridViewComboBoxCell is not valid" after setting the value

0 comments:

Post a Comment