组合框显示对象儿童成员

时间:2013-12-04 20:06:07

标签: c# entity-framework combobox

我正在尝试使用对象列表填充ComboBox。列表中的每个对象都有另一个对象。我需要在comboBox DisplayMember中显示“option.Dish.dish_name”,我需要在comboBox ValueMember中使用“option.option_id”。这是我正在尝试做的事情:

foreach (Option option in optionList)
{
        this.comboBox1.Items.Add(option);
        this.comboBox1.DisplayMember = "Dish.dish_name";
        this.comboBox1.ValueMember = "option_id";
 }

选项类是实体框架实体,但类似于:

public class Option 
{
    private int option_id;
    private DateTime option_date;
    private Dish dish;
}

public class Dish
{
    private int dish_id;
    private String dish_name;
    private DateTime date_created;
}

提前致谢。

修改

新问题:无论如何都要让“新对象”以后再使用它?我需要另一个值,例如:

this.comboBox1.ItemsSource = optionList.Select(o=> new
{
    option_id = o.option_id, 
    dish_name = o.dish.dish_name, 
    date = o.dish_date
}).ToList(); 

然后使用该对象获取“日期”值?。

1 个答案:

答案 0 :(得分:0)

this.comboBox1.ItemsSource =
        optionList.Select(o=> new{option_id = o.option_id, dish_name =o.dish.dish_name}).ToList();

this.comboBox1.DisplayMember = "dish_name";
this.comboBox1.ValueMember = "option_id";
相关问题