DataGridViewComboBoxColumn bound to complex object

I have a list ofCustomer objects(List<Customer>) bound to a DataGridView. One property of Customer isCategory which itself is an object of typeCategory. I want to bind this property to aDataGridViewComboBoxColumn column.
At design time, I add aDataGridViewComboBoxColumn column to the DataGridView. I set the following properties of this column.


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


TheBindingSource CategoryBindingSource is bound to aList<Category> object. But when I run it, I got two errors for each row for the DataGridView. The context isFormatting andDisplay. The error message is thecell 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 aCategoryId property in theCustomer 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 sourceList<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!
[2102 byte] By [blackpuppy] at [2007-12-16]
# 1

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 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
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 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
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 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
Yes, it looks really strange! But it does work!
Thanks a lot, matt!
blackpuppy at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

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

CarlosLh at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
Is there possible to have a multi-column combobox in the DataGridView?
nkotb at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...