2011-12-21 181 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");     
}