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

3

6

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

King answered 15/4, 2011 at 17:34 Comment(0)
B
8

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;     
}
Belter answered 15/4, 2011 at 17:47 Comment(1)
This works perfect @Belter !! Thank you very much. And yes I simplified my problem, but in my real example, Parent can only have one child. Thanks againKing
C
0

That will do the job:

Dictionary<String, String> children = new Dictionary<String, String>();
children["666"] = "Show THIS";

comboBox1.DataSource = children;
comboBox1.DataBind();

If Children was in a parent class, then you can simply use:

comboBox1.DataSource = parent.Children;
...

However, if you need to bind to the children of multiple parents you can do the following:

var allChildren =
   from parent in parentList
   from child in parent.Children
   select child

comboBox1.DataSource = allChildren;
Cristencristi answered 15/4, 2011 at 17:42 Comment(0)
T
0

You could just intercept the datasource changed event and do specific object bindings in there.

Theodosia answered 26/3, 2012 at 15:53 Comment(1)
Welcome to Stack Overflow! When answering questions, please provide details on your solutions so they're most useful to the question asker. Thanks!Myotome

© 2022 - 2024 — McMap. All rights reserved.