组合框的加载不显示选定的值

时间:2013-06-12 12:51:29

标签: c# .net winforms

我在表单上有一个组合框 - “cboCurPartNum”。当我打开表单并且现有数据填充表单时,我的组合框不会显示存储在SQL Server中的值。

如何让我的组合框在我的记录中显示值?

这里发生的是在“修改工程订单”模式下打开表单。所以,声明

public EngOrd engord;

包含正在打开的工程订单的数据。

问题出在“修改工程订单”部分的"addEngOrd" = false。您将看到调用方法“PutNewEngOrd()”并为newEngOrd对象分配已打开的工程订单的现有值。

我的问题是组合框“cboCurPartNum”,其中包含以下内容:

Data Source - partBindingSource
Display Member - PartNumber
Value Member - PartNumber
Selected Value - engOrderBindingSource - CurPartNum

此组合框应分配“newEngOrd”中的partnumber,因为“engordBindingSource.Add(newEngOrd);”在“PutNewEngOrd()”运行后被调用。

为什么我的部件号不会显示在组合框中?部件号存储在数据库中。

这是我的代码:

public partial class frmEngOrdAddModify : Form
{
    public frmEngOrdAddModify()
    {
        InitializeComponent();            
    }

    public bool addEngOrd;  //indicates if adding to the form or not
    public EngOrd engord; //data from the frmEngOrd (old eng order)
    private EngOrd newEngOrd;  //new data we are capturing here (new eng ord)


    private void frmEngOrdAddModify_Load(object sender, EventArgs e)
    {
        this.LoadComboBoxes();           

        if (addEngOrd)
        {
            this.Text = "Add Engineering Order";
            newEngOrd = new EngOrd();
            engordBindingSource.Clear();
        }
        else
        {
            this.Text = "Modify Engineering Order";
            newEngOrd = new EngOrd();                
            this.PutNewEngOrd();
        }
        engordBindingSource.Add(newEngOrd);  //put the existing or a new EngOrd dataset with the binding source


    }

    private void LoadComboBoxes()
    {

        List<EngOrdType> engOrdTypeList;
        engOrdTypeList = EngOrdTypeDB.GetEngOrdType();
        cboEOFileType.DataSource = engOrdTypeList;
        cboEOFileType.SelectedIndex = -1;

        List<Customer> customerList;
        customerList = CustomerDB.GetCustomerTeamList();
        cboCustomer.DataSource = customerList;
        cboCustomer.SelectedIndex = -1;

        List<Part> partList;
        partList = PartDB.GetPartList();
        cboCurPartNum.DataSource = partList;            
        cboCurPartNum.SelectedIndex = -1;

        // with modify mode intially display the value passed in from engord
        if (addEngOrd == false)
        {
            MessageBox.Show("assign here " + engord.CurPartNum);
            cboCurPartNum.SelectedValue = engord.CurPartNum;
        }
    }

    private void PutNewEngOrd()
    {
        newEngOrd.EO = engord.EO;
        newEngOrd.EONum = engord.EONum;
        newEngOrd.FileType = engord.FileType;
        newEngOrd.JobNum = engord.JobNum;
        newEngOrd.QuoteNum = engord.QuoteNum;
        newEngOrd.CustID = engord.CustID;
        newEngOrd.CurPartNum = engord.CurPartNum;            

    }

}

这是“cboCurPartNum”

的设计器生成代码
this.cboCurPartNum.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.engordBindingSource, "CurPartNum", true));
this.cboCurPartNum.DataSource = this.partBindingSource;
this.cboCurPartNum.DisplayMember = "PartNumber";
this.cboCurPartNum.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboCurPartNum.FormattingEnabled = true;
this.cboCurPartNum.Location = new System.Drawing.Point(142, 23);
this.cboCurPartNum.Name = "cboCurPartNum";
this.cboCurPartNum.Size = new System.Drawing.Size(179, 21);
this.cboCurPartNum.TabIndex = 11;
this.cboCurPartNum.ValueMember = "PartNumber";
this.cboCurPartNum.SelectionChangeCommitted += new System.EventHandler(this.cboCurPartNum_SelectionChangeCommitted_1)

这就是我现在拥有的: 这就是我现在拥有的。设置SelectedValue需要什么?

        private void LoadComboBoxes()
    {

        List<EngOrdType> engOrdTypeList;
        engOrdTypeList = EngOrdTypeDB.GetEngOrdType();
        cboEOFileType.DataSource = engOrdTypeList;
        cboEOFileType.SelectedIndex = -1;

        List<Customer> customerList;
        customerList = CustomerDB.GetCustomerTeamList();
        cboCustomer.DataSource = customerList;
        cboCustomer.SelectedIndex = -1;
        //MessageBox.Show(cboCustomer.SelectedItem.ToString());
        //MessageBox.Show(cboCustomer.SelectedValue.ToString());

        List<Part> partList;
        partList = PartDB.GetPartList();
        cboCurPartNum.DataSource = partList;
        cboCurPartNum.SelectedIndex = -1;
        if (addEngOrd == false)
        {
            cboCurPartNum.SelectedItem = partList[1];

        }

    }

2 个答案:

答案 0 :(得分:1)

cboCurPartNum.SelectedValue = / 这里syou应该设置一个“Part”类型的项目。例如 partList [0] /

public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      LoadComboboxes();
    }

    public void LoadComboboxes() {
        List<Part> partList = PartDB.GetPartList();
        cboCurPartNum.DataSource = partList;
        cboCurPartNum.SelectedIndex = -1;
        var addEngOrd = false;
        if (addEngOrd  == false) {
          //MessageBox.Show("assign here " + 1);
          cboCurPartNum.SelectedItem = partList[1];
        }
    }
  }
  public static class PartDB {
    static PartDB() {
      PartList = new List<Part> {
        new Part {
          One = 1,
          Two = "unos"
        },
        new Part {
          One = 2,
          Two = "duos"
        }
      };
    }

    private static readonly List<Part> PartList;
    public static List<Part> GetPartList() {
      return PartList;
    }
  }

  public class Part {
    public int One { get; set; }
    public string Two { get; set; }
  }
}

另外,请确保将DisplayMember设置为您要在组合框中看到的属性(请参阅附件) enter image description here

答案 1 :(得分:0)

我看不到你为你的ComboBox设置DisplayMember属性的位置 - 是否在Designer生成的代码中? 尝试以编程方式设置DisplayMember属性,并重新检查DisplayMember值是否与要显示的Part类属性的名称匹配。