组合框选中的项目为空,不显示DB值

本文关键字:显示 DB 项目 组合 | 更新日期: 2023-09-27 18:18:53

我有一个组合框cboPrePartNum:

  • 数据源- prepartBindingSource
  • Display Member - PrePartNum
  • Value Member - 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; }
    }
}

组合框选中的项目为空,不显示DB值

您不需要在组合框中设置Selected Value。您可以像下面的代码

那样获取和设置选中的项
 cboPrePartNum.SelectedItem = <PartPrevioustype of object>;
 <PartPrevioustype of object> = cboPrePartNum.SelectedItem;

你应该设置

cboPrePartNum.DataTextField  

cboPrePartNum.DataValueField

那么它就会正确绑定