Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a ComboBox to a generic List with deep DisplayMember and ValueMember properties?

I am trying to bind a generic list like List Parents to a ComboBox.

    public Form1()
    {
        InitializeComponent();
        List<Parent> parents = new List<Parent>();
        Parent p = new Parent();
        p.child = new Child();
        p.child.DisplayMember="SHOW THIS";
        p.child.ValueMember = 666;
        parents.Add(p);
        comboBox1.DisplayMember = "child.DisplayMember";
        comboBox1.ValueMember = "child.ValueMember";
        comboBox1.DataSource = parents;
    }
}
public class Parent
{
    public Child child { get; set; }
}
public class Child
{
    public string DisplayMember { get; set; }
    public int ValueMember { get; set; }
}

When I run my test app I only see: "ComboBindingToListTest.Parent" displayed in my ComboBox instead of "SHOW THIS". How can I bind a ComboBox to a Generic List through one level or deeper properties e.g. child.DisplayMember??

Thanks in Advance, Adolfo

like image 344
Adolfo Perez Avatar asked Apr 15 '11 17:04

Adolfo Perez


People also ask

How do I bind a ComboBox to a list?

To bind a ComboBox or ListBox control If you are binding to a table, set the DisplayMember property to the name of a column in the data source. If you are binding to an IList, set the display member to a public property of the type in the list.

What is DisplayMember and ValueMember in C#?

DisplayMember : To display the underlying datasource for Windows Forms ComboBox. ValueMember : To use as the actual value for the items.

Can not bind to the new display member?

As it turns out, one of the selections in the 1st combo box did not have any data to display in the 2nd combo box. When this happened, my code was trying to set the ValueMember for the 2nd combo box when there was no data in the DataSource. This is what caused the "could not bind..." error for me.

Is the default event of ComboBox control?

By default, DropDownStyle property of a Combobox is DropDown. In this case user can enter values to combobox. When you change the DropDownStyle property to DropDownList, the Combobox will become read only and user can not enter values to combobox.


1 Answers

I don't think you can do what you ar attempting. The design above shows that a Parent can only have one child. Is that true? Or have you simplified the design for the purpose of this question.

What I would recommend, regardless of whether a parent can have multiple children, is that you use an anonymous type as the Data Source for the combo box, and populate that type using linq. Here is an example:

private void Form1_Load(object sender, EventArgs e)
{
    List<Parent> parents = new List<Parent>();
    Parent p = new Parent();
    p.child = new Child();
    p.child.DisplayMember = "SHOW THIS";
    p.child.ValueMember = 666;
    parents.Add(p);

    var children =
        (from parent in parents
            select new
            {
                DisplayMember = parent.child.DisplayMember,
                ValueMember = parent.child.ValueMember
            }).ToList();

    comboBox1.DisplayMember = "DisplayMember";
    comboBox1.ValueMember = "ValueMember";
    comboBox1.DataSource = children;     
}
like image 184
essedbl Avatar answered Sep 22 '22 05:09

essedbl