组合框选择项目为null并且不显示DB值

时间:2013-06-18 03:28:40

标签: c# .net winforms

我有一个组合框cboPrePartNum

  • 数据源 - prepartBindingSource
  • 展示会员 - PrePartNum
  • 超值会员 - PrePartNum
  • 选定值 - engordbindingsource - prepartnum

当表单加载时,运行:

private void LoadComboBoxes()
{
    List<PartPrevious> partPreviousList;
    partPreviousList = PartDB.GetPartPreviousList();
    cboPrePartNum.DataSource = partPreviousList;
    cboPrePartNum.SelectedIndex = -1;
}

问题是表单加载时cboPrePartNum组合框不会显示当前engordbindingsource.prepartnum中的值。

这就是PartPrevious的样子:

public class PartPrevious
{
    private string prepartnum;
    private string prerevision;
    private string prepartdescription;

    public PartPrevious() { }

    public string PrePartNum
    {
        get { return prepartnum; }
        set { prepartnum = value; }
    }

    public string PreRevision
    {
        get { return prerevision; }
        set { prerevision = value; }
    }

    public string PrePartdescription
    {
        get { return prepartdescription; }
        set { prepartdescription = value; }
    }
}

2 个答案:

答案 0 :(得分:1)

您无需在组合框中设置“选定值”。您可以获取和设置所选项目,如下面的代码

 cboPrePartNum.SelectedItem = <PartPrevioustype of object>;

 <PartPrevioustype of object> = cboPrePartNum.SelectedItem;

答案 1 :(得分:0)

你应该设置

cboPrePartNum.DataTextField  

cboPrePartNum.DataValueField

然后它会正确绑定