2011-12-21 117 views
1

我有一個窗體上的組合框控件,它從某些數據源中提取其數據(顯示和值)。在另一邊,我有一排桌子。我希望當應用程序正在開發時,組合框將selectedvalue或selecteditem設置爲上一行中一列的值。當用戶更改組合框時,它將保持更改爲行。我試圖將SelectedValue綁定到這個列,但它不起作用。 Combobox僅設置開始到第一個項目。什麼是問題?組合框數據綁定


編輯

這是一個雙贏的窗體項目。 這裏是綁定代碼:

this.comboBoxCountries = new System.Windows.Forms.ComboBox(); 
this.countriesBindingSource = new System.Windows.Forms.BindingSource(this.components); 

// 
// comboBoxCountries 
// 
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.searchCriteriaBindingSource, "Postcode", true)); 
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.searchCriteriaBindingSource, "CountryCode", true)); 
this.comboBoxCountries.DataSource = this.countriesBindingSource; 
this.comboBoxCountries.DisplayMember = "Name"; 
this.comboBoxCountries.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
this.comboBoxCountries.FormattingEnabled = true; 
this.comboBoxCountries.Location = new System.Drawing.Point(190, 19); 
this.comboBoxCountries.Name = "comboBoxCountries"; 
this.comboBoxCountries.Size = new System.Drawing.Size(156, 21); 
this.comboBoxCountries.TabIndex = 2; 
this.comboBoxCountries.ValueMember = "Code"; 
this.comboBoxCountries.SelectedValueChanged += new System.EventHandler(this.comboBoxCountries_SelectedValueChanged); 

// 
// countriesBindingSource 
// 
this.countriesBindingSource.DataMember = "Countries"; 
this.countriesBindingSource.DataSource = this.dbDataSetCountries; 
// 
// dbDataSetCountries 
// 
this.dbDataSetCountries.DataSetName = "dbDataSetCountries"; 
this.dbDataSetCountries.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; 

// 
// searchCriteriaBindingSource 
// 
this.searchCriteriaBindingSource.AllowNew = false; 
this.searchCriteriaBindingSource.DataMember = "SearchCriteria"; 
this.searchCriteriaBindingSource.DataSource = this.dbDataSetSearchCriteria; 
this.searchCriteriaBindingSource.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.searchCriteriaBindingSource_BindingComplete); 
// 
// dbDataSetSearchCriteria 
// 
this.dbDataSetSearchCriteria.DataSetName = "dbDataSetSearchCriteria"; 
this.dbDataSetSearchCriteria.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; 

EDIT2

正如我在我下面的評論中提到,我還有一個textbox被綁定到相同綁定源的其他DataMembertextbox工作精細。它具有適當的價值。當我在同一個數據庫上更改DataMember時,我在其中設置了組合框的selectedvalue屬性,它也顯示出良好的結果並正常工作。

在此先感謝!

+0

嘗試把,如果結合中(是!回發)功能 – prema 2011-12-21 04:59:34

+0

哪種技術是winforms/wpf/asp.net?你試過的代碼是什麼? – Maheep 2011-12-21 05:00:14

+0

請發佈您的數據綁定代碼(ItemsSource和SelectedItem/Value)以及數據源定義。 – 2011-12-21 05:02:31

回答

4

看看DisplayMemberValueMember組合框的屬性。您需要告訴ComboBox下拉列表中要顯示的數據源中的成員以及請求SelectedValue時要提供的值。

這聽起來像你的ComboBox綁定到一個靜態列表,而你的行不是。您可以考慮使用BindingSource來設置ComboBox和DataGridView的DataSource。這樣,當DGV導航到新行時,ComboBox將更新爲新行的值。

下面是對ComboBox的鏈接MSDN:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

+0

請看我編輯的問題。 – kseen 2011-12-22 08:40:55

3

我找到它。因此,對於這個問題,您應該刪除從綁定的菜單視覺工作室數據的SelectedValue綁定,並把相應的代碼在一些地方填充所有bindingsources後添加此數據綁定管理:

private void MainForm_Load_1(object sender, EventArgs e) 
{ 
    this.searchCriteriaTableAdapter1.Fill(this.dbDataSetCountries.SearchCriteria);  

    this.searchCriteriaTableAdapter.Fill(this.dbDataSetSearchCriteria.SearchCriteria);  

    comboBoxCountries.DataBindings.Add("SelectedValue", this.dbDataSetCountries.SearchCriteria, "CountryCode");     
}