Search code examples
c#comboboxvaluemember

Set object's nested object's proerty as combo valuemember


I have a combobox. Its datasource a list of objects. This class has a public property that is a class defined by me. I would like to set combo's valuemember to a property of this class. How can I do that?

class B
{
public int Id {get; set;}
}

class A
{
public B AnyProperty {get; set;}
public string Name {get; set;}
}

var testList = new List<A>() {...};
combo.DataSource = testList;
combo.DisplayMemver = "Name";
combo.ValueMember = ??? //it should be A.AnyProperty.Id

.Net4/VS2010/C#

Thx


Solution

  • combo.ValueMember = "AnyProperty";
    

    Combobox will bind an object. You can then get the value back by doing something like this.

    var selectedValue = (B)combobox.SelectValue;
    

    Please not that the above sytax maybe be incorrect.