我有一个C#Windows窗体应用程序,并且在窗体加载方法中,我加载了一些组合框。对于其中之一,MeasureType,它显示的是ValueMember字段,这只是一个无意义的ID。我想要MeasureType描述(MeasureType1)。我注意到,如果刷新或调用函数以两次(背对背)加载组合框,则它将起作用并显示说明字段。我尝试按照下面的代码中分配属性的顺序设置DataSource的第一个和最后一个。所有其他组合框均按预期工作。有人知道我在做什么错吗?
private void MeasureForm_Load(object sender, EventArgs e)
{
LoadForm();
LoadForm(); //if I call twice, it shows DisplayMember field
}
private void LoadForm()
{
measLoader = new MeasureData();
cbCurrentMeasures.DataSource = measLoader.GetMeasuresWithEmpty();
cbCurrentMeasures.DisplayMember = "MeasureName";
cbCurrentMeasures.ValueMember = "MeasureID";
//*** THIS ONE IS NOT SHOWING DISPLAYMEMBER ***
cbTypes.DataSource = measLoader.GetMeasureTypes();
cbTypes.DisplayMember = "MeasureType1";
cbTypes.ValueMember = "MeasureTypeID";
cbMethods.DataSource = measLoader.GetWebServiceMethodsWithEmpty();
cbMethods.DisplayMember = "MethodName";
cbMethods.ValueMember = "WebServiceMethodID";
cbProperties.DataSource = measLoader.GetClassPropertiesWithEmpty();
cbProperties.DisplayMember = "DisplayField";
cbProperties.ValueMember = "PropertyID";
chklistItems.DataSource = measLoader.GetItems();
((ListBox)chklistItems).DisplayMember = "ItemName";
((ListBox)chklistItems).ValueMember = "ItemID";
}
private void cbTypes_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbTypes.Text.Contains("QI"))
{
cbProperties.Enabled = false;
cbProperties.SelectedIndex = 0;
}
else
{
cbProperties.Enabled = true;
}
}
使用linq-to-sql的MeasurementData类:
public List<MeasureType> GetMeasureTypes()
{
return db.MeasureTypes.ToList<MeasureType>();
}
答案 0 :(得分:0)
由于cbMeasureTypes上的事件处理程序试图在没有任何数据之前设置cbProperties的SelectedIndex,因此将cbProperties的加载移到了cbMeasureTypes之前。