items属性的combo_绑定文本框到comboBox.SelectedItem属性

I'm using winforms and I've got a comboBox that represents an IQueryable. Below the combobox is a series of textboxes that I would like to be bound to the currently selected from the combo box.

Here is my code.

public partial class TestForm : Form

{

public DataClassesDataContext DataContext;

public IQueryable datasource;

// Ctor

public TestForm()

{

InitializeComponent();

// L2S data context

this.DataContext = new DataClassesDataContext();

// Get the variable for the data source

this.datasource = this.DataContext.Ts;

// setup the binding for the combobox

this.comboBox1.DataSource = this.datasource;

this.comboBox1.DisplayMember = "Description";

this.comboBox1.ValueMember = "Id";

// assign the databindings of the text boxes to the selectedItem of the combo box

// this is where the problem is, afaik

TId.DataBindings.Add(new Binding("Text", this.comboBox1.SelectedItem, "Id"));

TUser.DataBindings.Add(new Binding("Text", this.comboBox1.SelectedItem, "User"));

TDescription.DataBindings.Add(new Binding("Text", this.comboBox1.SelectedItem, "Description"));

}

Doing this binds everything, When I change the values in the text boxes, it updates the initially selected item in the combo box just fine. Even when I change the description, it updates the displayed text in the drop don, all that is great.

However, when I select a different item from the drop down, the text boxes don't bind to that newly selected item, they stay bound to the old one.

Do I need to remove and re-add my bindings every time the selection changes on the combo box?

解决方案

Use a BindingSource rather than directly relying upon the L2S data context. The binding source uses a concurrency manager to handle all the updating for you and the L2S does not

Working code:

public partial class TestForm : Form

{

public DataClassesDataContext DataContext;

// Incorrect: public IQueryable datasource;

// Correct:

public BindingSource TsDataSource;

// Ctor

public TestForm()

{

InitializeComponent();

// L2S data context

this.DataContext = new DataClassesDataContext();

// Get the variable for the data source

// Incorrect: this.datasource = this.DataContext.Ts;

// Correct:

this.TsDataSource = new BindingSource();

this.TsDataSource.DataSource = this.DataContext.Ts;

// setup the binding for the combobox

this.comboBox1.DataSource = this.TsDataSource;

this.comboBox1.DisplayMember = "Description";

this.comboBox1.ValueMember = "Id";

// assign the databindings of the text boxes to the selectedItem of the combo box

TId.DataBindings.Add(new Binding("Text", this.TsDataSource, "Id"));

TUser.DataBindings.Add(new Binding("Text", this.TsDataSource, "User"));

TDescription.DataBindings.Add(new Binding("Text", this.TsDataSource, "Description"));

}

More about BindingSource from the source (couldnt resist):

The BindingSource component serves

many purposes. First, it simplifies

binding controls on a form to data by

providing currency management, change

notification, and other services

between Windows Forms controls and

data sources. This is accomplished by

attaching the BindingSource component

to your data source using the

DataSource property. For complex

binding scenarios you can optionally

set the DataMember property to a

specific column or list in the data

source. You then bind controls to the

BindingSource. All further interaction

with the data is accomplished with

calls to the BindingSource component.

For examples on how the BindingSource

can simplify the binding process, see

How to: Bind Windows Forms Controls to

DBNull Database Values and How to:

Handle Errors and Exceptions that

Occur with Databinding. Navigation and

updating of the data source is

accomplished through methods such as

MoveNext, MoveLast, and Remove.

Operations such as sorting and

filtering are handled through the Sort

and Filter properties. For more

information on using sorting and

filtering with the BindingSource, see

How to: Sort and Filter ADO.NET Data

with the Windows Forms BindingSource

Component.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值