组合框数据绑定 [英] ComboBox data binding

查看:128
本文介绍了组合框数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据源拉它的数据(显示和值)上形成一个组合框控件。在另一边我有一排桌子。我想,当应用程序是空空,组合框设置了selectedValue或selectedItem上文行一列的值。当用户改变组合框,将持续改变排。我试图绑定的SelectedValue此列,但它不工作。组合框只是设置上开始第一个项目。什么是问题?

I have a combobox control on form that pull its data (Displays and values) from some datasource. On another side I have table with one row. I want when app is lauching, combobox set selectedvalue or selecteditem to value of one column in above row. And when user has changed combobox it will persist change to row. I have tried to bind SelectedValue to this column, but it doesn't work. Combobox just sets on start to first item. What is problem?

修改

这是一个双赢的窗体项目。
下面是绑定代码:

This is a Win Forms project. Here is the binding code:

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

我在我下面的评论中提到,我还有一个文本被绑定到另一个数据成员相同的绑定源和文本工作正常的。它的出现与适当的值。当我更改数据成员上,我将的SelectedValue 组合框的属性绑定它也表现出了良好的效果和工作一样的DataMember正常。

As I have mentioned in my comment below, I have another textbox that is binded to another DataMember of same binding source and textbox working fine. It's appear with appropriate value. When I change DataMember on same datamember on which I set selectedvalue property of combobox binding it's also show a good result and work properly.

在此先感谢!

推荐答案

看看在的DisplayMember ValueMember 组合框的属性。你需要告诉组合框从数据源是什么成员在下拉显示,和什么样的价值要求的SelectedValue时候给。

Take a look at the DisplayMember and ValueMember properties of the combobox. You need to tell the ComboBox what member from the datasource to display in the drop down, and what value to give when SelectedValue is requested.

这听起来像你的组合框是绑定以静态列表,而你行不。您可以考虑使用您设置的组合框,并在DataGridView的DataSource到的BindingSource。 。这样,当DGV导航到一个新行,组合框将与新行的值更新

It sounds like your ComboBox is bound to a static list while your rows are not. You might consider using a BindingSource that you set the ComboBox and the DataGridView's DataSource to. That way when the DGV navigates to a new row, the ComboBox will be updated with the value for the new row.

下面是MSDN上的组合框的链接:< A HREF =http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx相对=nofollow> http://msdn.microsoft.com/en-us/库/ system.windows.forms.combobox.aspx

Here is a link to the ComboBox on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

这篇关于组合框数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆