gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:46:28 27 4
gpt4 key购买 nike

我在表单上有一个组合框控件,它从某个数据源中提取数据(显示和值)。在另一边,我有一行表。我希望在应用程序启动时,组合框将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com