Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2004 >

dotnet windows forms databinding : Databinding two controls together - works?


BBM
11/1/2004 7:58:04 AM
Why doesn't this work? Very simple code...

nvl.BindableList is an ArrayList of "NameValue" objects
Name and Value are object properties of the NameValue objects
cbxDesType is a combo-box
tbxDesTypeCode is a textbox

private void frmDesTypeTest_Load(object sender, System.EventArgs e)
{
this.cbxDesType.DisplayMember = "Value";
this.cbxDesType.ValueMember = "Name";
this.cbxDesType.DataSource = nvl.BindableList;
this.tbxDesTypeCode.DataBindings.Add ("Text",cbxDesType.SelectedValue,
null);
}

When initially displayed, the TextBox shows the correct code for the
selected value of the combo-box (the first value). As the selected combo-box
value changes the text in the TextBox never changes.

I can obviously make it work by trapping the combobox.SelectedValue changed
event, and then setting the TextBox.Text property equal to the SelectedValue
but isn't that what DataBinding is supposed to accomplish?

Thanks.

Mike Edenfield
11/1/2004 1:21:26 PM
[quoted text, click to view]

I can't get this code to work even that much over here, so I'm just
guessing. Does it help if you bind the controls to each other like this:

tbxDesTypeCode.DataBindings.Add ("Text", cbxDesType, "SelectedValue");

Note that, to acheive the same result (if that's what you're after),
binding the text box to the data data source as the combo box would
work. In that case, the form's currency manager would keep all bound
controls looking at the same record in the data source.

this.cbxDesType.DisplayMember = "Value";
this.cbxDesType.ValueMember = "Name";
this.cbxDesType.DataSource = nvl.BindableList;
this.tbxDesTypeCode.DataBindings.Add ("Text", nvl.BindableList, "Name");

--Mike
v-jetan NO[at]SPAM online.microsoft.com (
11/2/2004 2:25:44 AM
Hi BBM,

Based on my understanding, you want to keep a combobox and textbox with the
winform databinding synchronizely.

Actually, you misunderstanded the concept of databinding. For winform
simple databinding, a control's property is bound to a datasource's
datamember(Such as a property). While 2 controls bound to the same
datasource, winform databinding will keep the synchronization for these 2
controls while databinding.

But in your code below:
this.tbxDesTypeCode.DataBindings.Add ("Text",cbxDesType.SelectedValue,
null);
You bind TextBox.Text property to SelectedValue, so the combobox and
TextBox are bound to 2 different datasouce, and there is no synchronization
or between these 2 databindings. When we change the selected item in
combobox, the textbox has nothing knowledge of the change, so its text will
not change at all.

The correct way to keep the synchronization is set the same
datasource(nvl.BindableList) for both combobox and textbox, then set
"Value" as Binding class's third parameter.

I have writen a sample like below:
public class customized_class
{
private int m_intval=0;
public int intval
{
get
{
return m_intval;
}
set
{
m_intval =value;
}
}

private string m_strval="aaa";
public string strval
{
get
{
return m_strval ;
}
set
{
m_strval =value;
}
}
}

private customized_class[] arr;
private void Form1_Load(object sender, System.EventArgs e)
{
arr=new customized_class[3];
for(int i=0;i<3;i++)
{
arr[i]=new customized_class();
arr[i].intval=i;
arr[i].strval=i.ToString()+"setvalue";
}
this.comboBox1.DisplayMember="strval";
this.comboBox1.ValueMember="intval";
this.comboBox1.DataSource=arr;

this.textBox1.DataBindings.Add(new Binding("Text", arr, "strval"));
}
It works well on my side(with synchronization).
=====================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
BBM
11/2/2004 4:31:02 AM
Thanks Mike.

I am aware that I can bind the two controls to the same DataSource and make
this work. This piece of code actually came from a test panel I was using.
I wanted to validate that the value set by the combo-box change was correct,
and thought I might be able to bind another control to the combo-box
properties just like any other source.

Apparently not.

[quoted text, click to view]
BBM
11/2/2004 4:43:05 AM
Thanks Jeffrey.

My question is why CAN'T I use the properties one control as the bound
DataSource of another.

There's a perfectly good reason to do this (to me at least). In my case I
was writing a test panel and wanted to confirm that the value of the
combo-box was being set properly. It's not that I wanted to synchronize the
two controls per se, I was interested in what SelectedValue was for the
ComboBox.

After thinking about this some, it's probably because the WinForms Combo-box
Properties don't fire the <property>changed events that DataBinding needs to
do two-way binding to an object. So when the Combo-box.SelectedValue
changes, Databinding doesn't pick it up.

Is this correct, or is there another reason?

Thanks.

BBM

[quoted text, click to view]
v-jetan NO[at]SPAM online.microsoft.com (
11/3/2004 7:28:29 AM
Hi BBM,

Thanks very much for your feedback!!

Oh, I see that you just wanted to use code to reflect out the selected
value change behavior of combobx.

In this situation, databinding is not suitable for doing this. The correct
way is handling combobox1's SelectedValueChanged event, in this event, just
show out the combobox1's selectedvalue, this is the new selectedvalue.
==========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
AddThis Social Bookmark Button