2013-06-12 68 views
1

我在窗體上有一個組合框 - 「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]; 

     } 

    } 
+0

確保數據源是不是空的或空的。爲了能夠在該組合框中顯示選定的值,您應該已經將數據源填充到從中選擇了該項目的集合 – meorfi

+0

組合框具有來自數據源「partBindingSource」的值。存儲在數據庫中的值是數據源中的有效值。 – maverick07281975

+0

@Nancy你確定'CurPartNum'和'PartNumber'的數據類型是否相同?他們應該是一樣的。我想'CurPartNum'的所有​​可能值都包含在'PartNumber'列中,​​如果不是這樣的話。 –

回答

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

+0

爲完整的解決方案寫信給我:) – meorfi

+0

我該怎麼寫你?我是這個論壇的新手。我如何設置SelectedValue? – maverick07281975

+1

我找到了問題。在partBindingSource中有數據回車。這些數據來自另一個系統。 – maverick07281975

0

我看不到爲您設置DisplayMember屬性的位置ComboBoxes - 它是否在Designer生成的代碼中? 嘗試以編程方式設置DisplayMember屬性,並重新檢查DisplayMember值是否與需要顯示的Part類屬性的名稱相匹配。

+0

中的一個值進行保存。另外 - 對ComboBoxes查看值做另一個?該問題是否僅顯示與cboCurPartNum組合框? –

+0

其他ComboBoxes正在查看值。 DisplayMember正在設計器生成的代碼中設置。 – maverick07281975

+0

該問題僅適用於cboCurPartNum – maverick07281975