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

But at least I don't have to pass an ID and then search for the matching object.
Cheers