1
votes

I have a combo box on a form - "cboCurPartNum". When I go to open the form and the existing data populates the form, my combo box does not display the value stored in SQL server.

How can I get my Combo Box to display the value in my record?

What is happening here is the form is opened in the "Modify Engineering Order" mode. So, the statement

public EngOrd engord;

contains the data for the engineering order that is being opened.

The issue is where "addEngOrd" = false in the "Modify Engineering Order" section. You will see there the method "PutNewEngOrd()" is called and the newEngOrd object gets assigned the existing values of the engineering order opened.

My problem is with the combo box "cboCurPartNum" which has the following set:

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

This combo box should have the partnumber in "newEngOrd" assigned, because "engordBindingSource.Add(newEngOrd);" gets called after "PutNewEngOrd()" was run.

Why doesn't my part number display in the combo box? The part number is stored in the database.

Here is my code:

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;            

    }

}

This is the designer generated code for "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)

This is what I have now: This is what I have now. What do I need to the setting the 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
make sure that the DataSource is not Null or Empty. To be able to show the Selected Value in that combobox you should have filled the DataSource with the collection from which the SelectedItem is takenmihai
The combo box has values from the datasource "partBindingSource". The value stored in the database is a valid value in the datasource.maverick07281975
@Nancy Are you sure the data types of CurPartNum and PartNumber are the same? they should be the same. I supposed all the possible values of CurPartNum are contained in PartNumber column, if not that's the problem.King King
The data types are the same for CurPartNum and PartNumber. The CurPartNum initially gets saved using one of the values from the partBindingSource.PartNumbermaverick07281975

2 Answers

1
votes

cboCurPartNum.SelectedValue = /Here syou should set an item of type "Part". For Example 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; }
  }
}

also, Make sure the DisplayMember is set to the Property which you want to see in combobox (see attachment) enter image description here

0
votes

I can't see where you setting a DisplayMember property for yours ComboBoxes - is it in a Designer generated code? Try to set DisplayMember property programmaticly and recheck that DisplayMember value matches the name of Part class property needed to be displayed.