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];
}
}
CurPartNum
andPartNumber
are the same? they should be the same. I supposed all the possible values ofCurPartNum
are contained inPartNumber
column, if not that's the problem. – King King