组合框显示成员未更改

时间:2013-04-09 08:35:22

标签: c# entity-framework combobox

我在我的表单中使用了一个ComboBox,并使用Entity Framework作为其数据源,我的代码是:

            publishContext = new publishingCompanyEntities();
            comboBox2.DataSource = publishContext.Authors;
            comboBox2.DisplayMember = "FirstName";
            MessageBox.Show(comboBox2.DisplayMember.ToString());//this line return null !

虽然我更改了ComboBox的DisplayMember,但MessageBox返回null值, ComboBox没有填充数据,数据库有数据,publishContext.Authors返回它们,但ComboBox没有显示它们!

1 个答案:

答案 0 :(得分:2)

您需要像我提到的那样显示您的消息框。

publishContext = new publishingCompanyEntities();
comboBox2.DisplayMember = "FirstName";
comboBox2.DataSource = publishContext.Authors.ToList();
MessageBox.Show(publishContext.Authors.Count().ToString());

现在检查是否为null,如果为null,则使用try catch block进行精确错误

try
{
publishContext = new publishingCompanyEntities();
comboBox2.DisplayMember = "FirstName";
comboBox2.DataSource = publishContext.Authors.ToList();
MessageBox.Show(publishContext.Authors.Count().ToString());
}
catch(Exception ex)
{
}

或者您也可以查看此Link

希望它有效......