ComboBox数据绑定



我有一个表单上的组合框控件,它从一些数据源中提取其数据(显示和值)。在另一边,我有一张单排的桌子。我想当应用程序启动时,组合框设置selectedvalue或selecteditem值在上面一行的一列。当用户更改组合框时,它将保留更改到行。我试图将SelectedValue绑定到此列,但它不起作用。组合框只是设置在开始到第一个项目。问题是什么?


编辑

这是一个Win Forms项目。下面是绑定代码:

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与相同绑定源的另一个DataMember绑定,textbox工作良好。它似乎具有适当的价值。当我在相同的数据成员上更改DataMember时,我设置了组合框绑定的selectedvalue属性,它也显示出良好的结果并正常工作。

提前感谢!

看看组合框的DisplayMemberValueMember属性。你需要告诉ComboBox要在下拉框中显示数据源中的哪个成员,以及在请求SelectedValue时给出什么值。

这听起来像你的ComboBox绑定到一个静态列表,而你的行不是。你可以考虑使用一个BindingSource来设置ComboBox和DataGridView的数据源。这样,当DGV导航到新行时,组合框将使用新行的值进行更新。

以下是MSDN上的ComboBox链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

我发现了。因此,为了解决这个问题,你应该从visual studio数据绑定菜单中删除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");                
}

最新更新