我在表格上有一个组合框 - “ 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”中的部件号,因为“ 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];
}
}