Refresh related controls' value when data changed using DataBind in Winform

Goal: 

Use a ComboBox to select an item, and save the selected item's value to some textboxes. each control is using databinding.

Problems:

1. set control's value when comboBox changed, after the comboBox lost focus, the text was lost since the controls reload the data from binding source.

2. set binding source's value, the text will be set only when the comboBox lost focus. but we want to set the text immediately when the comboBox changed.

Solution:

set binding source's value, and call comboBox's writeValue for comboBox. this will write the comboBox's value to datasource, and reload all controls' value.

Test selectedTest = (Test)comboBox1.SelectedItem;

Test test = (Test)bindingSource1.DataSource;
if (selectedTest == null || test == null)
return;

//save the selected value to the binding source test.Name = selectedTest.Name; test.Value = selectedTest.Value; //this will write the comboBox's value to datasource, and reload all controls' value comboBox1.DataBindings["SelectedValue"].WriteValue();

posted on 2007-08-29 10:31  Wade  阅读(789)  评论(0编辑  收藏  举报

导航