Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewComboBoxColumn bound to complex object
 

DataGridViewComboBoxColumn bound to complex object

I have a list of Customer objects(List<Customer>) bound to a DataGridView.  One property of Customer is Category which itself is an object of type Category.  I want to bind this property to a DataGridViewComboBoxColumn column.

At design time, I add a DataGridViewComboBoxColumn column to the DataGridView.  I set the following properties of this column.



(pseudo code)
DataPropertyName = "Category"
DataSource = CategoryBindingSource
DisplayName = "Name"
ValueMemeber = (blank)

 


The BindingSource CategoryBindingSource is bound to a List<Category> object.  But when I run it, I got two errors for each row for the DataGridView.  The context is Formatting and Display.  The error message is the cell value is not valid.

Then I go to debug mode, and find the cell's ValueType is string instead of Category.  Why is it like that?

Then I add a CategoryId property in the Customer class which is a Guid.  This time I set the following properties of the combo column.



(pseudo code)
DataPropertyName = "CategoryId"
DataSource = CategoryBindingSource
DisplayName = "Name"
ValueMemeber = "Id"

 


Now it can run without an error.  But I find the combo column cannot be dropped down.  In fact, the data source List<Category> object contains two Category objects.

Does anybody has any experience with binding a DataGridViewCobmoColumn to a complex object (Category in my case) instead of a primitive data type (like Guid CategoryId)?  Do I have to add a CategoryId just for this problem?

Thanks!
blackpuppy  Thursday, August 04, 2005 4:37 AM
Hi,
I have been struggling with the same problem.

Out of desperation have come up with a fairly bizarre solution you may want to try out.

I'm my reference types I have a readonly property called Self, which simply returns me.



    Public ReadOnly Property Self() As MyType
        Get
            Return Me
        End Get
    End Property

 


So I'm my DataGridViewComboBoxColumn I set the valueMember to the datasource property Self and the displayMember to some display property (in my case "Name").

It works.

I'm not sure if its horrible or genius. Smile
But at least I don't have to pass an ID and then search for the matching object.

Cheers
sl1pm4t  Saturday, August 20, 2005 6:12 AM

Dear blackpuppy,

First, u have to create BindingSource

BindingSource _categoryBS = new BindingSource();
//_dsCategory contain data of Category
_categoryBS.DataSource = _dsCategory;
_categoryBS.DataMember =
"Category";

Second, load data in DataGridViewComboBoxColumn

grdStandardNotes.Columns.Add(GetComboBoxColumn("Category", "CategoryID", "CategoryName", "CategoryID", 5, 70, 100, _categoryBS));

/// <summary>
/// Add ComboBoxColumn in DataGridView with DataSource is BindingSource
/// </summary>
/// <param name="headerText"></param>
/// <param name="dataPropertyName"></param>
/// <param name="displayMember"></param>
/// <param name="valueMember"></param>
/// <param name="maxDropDownItems"></param>
/// <param name="dropDownWidth"></param>
/// <param name="width"></param>
/// <param name="bindingSource"></param>
/// <returns>DataGridViewComboBoxColumn</returns>
private DataGridViewComboBoxColumn GetComboBoxColumn(string headerText, string dataPropertyName, string displayMember, string valueMember, int maxDropDownItems, int dropDownWidth, int width, BindingSource bindingSource)
{
DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
comboBoxColumn.HeaderText = headerText;
comboBoxColumn.Name = dataPropertyName;
comboBoxColumn.DataPropertyName = dataPropertyName;
comboBoxColumn.DisplayMember = displayMember;
comboBoxColumn.ValueMember = valueMember;
comboBoxColumn.MaxDropDownItems = maxDropDownItems;
comboBoxColumn.DropDownWidth = dropDownWidth;
comboBoxColumn.Width = width;
comboBoxColumn.DataSource = bindingSource;
comboBoxColumn.DisplayStyle =
DataGridViewComboBoxDisplayStyle.Nothing;
return
comboBoxColumn;
}

Hope helpfull
Khiem Vo

khiemvo  Thursday, August 04, 2005 7:11 AM
Thanks, Khiem Vo!

To me, your example is too generic.  It looks not too much different from the example in MSDN, DataGridViewComboBoxColumn Class.  Do you have a more specific example?

I have two questions in my post.
1. When the data property is a reference type instead of a primitive value type such as Guid, why is there a Formatting/Display error?
2. When the data property is a primitive value type such as Guid, why the ComboBox column cannot be dorpped down?

Thanks!
blackpuppy  Thursday, August 04, 2005 7:29 AM
Hi,
I have been struggling with the same problem.

Out of desperation have come up with a fairly bizarre solution you may want to try out.

I'm my reference types I have a readonly property called Self, which simply returns me.



    Public ReadOnly Property Self() As MyType
        Get
            Return Me
        End Get
    End Property

 


So I'm my DataGridViewComboBoxColumn I set the valueMember to the datasource property Self and the displayMember to some display property (in my case "Name").

It works.

I'm not sure if its horrible or genius. Smile
But at least I don't have to pass an ID and then search for the matching object.

Cheers
sl1pm4t  Saturday, August 20, 2005 6:12 AM
Yes, it looks really strange!  But it does work!
Thanks a lot, matt!

blackpuppy  Monday, August 22, 2005 6:47 AM

I was struggling with the same issues as well, and at this point I could not bring myself to call it horrible. Thanx

CarlosLh  Wednesday, February 15, 2006 4:05 PM
Is there possible to have a multi-column combobox in the DataGridView??
nkotb  Saturday, March 18, 2006 6:30 AM

You can use google to search for other answers

Custom Search

More Threads

• What is better: DataTable / DataGridView with binding or just DataGridView with data supplied directly?
• How to get top 10 rows of data in a datagrid
• Help: Binding DataGrid Dynamically
• How to get an object value from a string???
• Reading datagrid values in a row
• datagridview duplicate values
• DataGrid headers
• change the value of two dimension array via DataGridView
• Updates to non-visible DataGridView rows impacting performance?
• Dataset Concurrency