c# - 组合框数据绑定(bind)

标签 c# .net data-binding combobox

我在表单上有一个组合框控件,它从某个数据源中提取数据(显示和值)。在另一边,我有一行表。我希望在应用程序启动时,组合框将 selectedvalue 或 selecteditem 设置为上一行中一列的值。当用户更改组合框时,它会将更改持久保存到行中。我试图将 SelectedValue 绑定(bind)到此列,但它不起作用。 Combobox 只是在开始时设置为第一项。有什么问题?


编辑

这是一个 Win Forms 项目。 这是绑定(bind)代码:

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 绑定(bind)到另一个具有相同绑定(bind)源和 textboxDataMember 工作美好的。它以适当的值出现。当我在设置组合框绑定(bind)的 selectedvalue 属性的相同数据成员上更改 DataMember 时,它也会显示良好的结果并正常工作。

提前致谢!

最佳答案

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

听起来您的 ComboBox 已绑定(bind)到静态列表,而您的行却没有。您可能会考虑使用您将 ComboBox 和 DataGridView 的数据源设置为的 BindingSource。这样,当 DGV 导航到新行时,ComboBox 将更新为新行的值。

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

关于c# - 组合框数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585243/

相关文章:

c# - 为什么枚举被编译为常量而不是静态值?

c# - 运行 Excel 加载项时,如果我之前的运行以断点结束,我总是会收到错误

.net - 在 MVC3 中排序时出现 "Cannot perform runtime binding on a null reference"错误

c# - 将 SelectedItems 绑定(bind)到 ObservableCollection 属性

ajax - 将 iron-ajax 数据绑定(bind)到模板

c# - 排除没有数据的对象

c# - 如何使用 Simple Injector 注册 Drum.UriMaker<>?

asp.net - 有没有办法确保在回收之前关闭应用程序池中的所有事件 session

c# - C# 中的后台线程

C# 通用应用程序 - 单击时的数据绑定(bind)