我在窗體上有一個組合框 - 「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];
}
}
確保數據源是不是空的或空的。爲了能夠在該組合框中顯示選定的值,您應該已經將數據源填充到從中選擇了該項目的集合 – meorfi
組合框具有來自數據源「partBindingSource」的值。存儲在數據庫中的值是數據源中的有效值。 – maverick07281975
@Nancy你確定'CurPartNum'和'PartNumber'的數據類型是否相同?他們應該是一樣的。我想'CurPartNum'的所有可能值都包含在'PartNumber'列中,如果不是這樣的話。 –