Groups | Blog | Home
all groups > dotnet windows forms databinding > december 2006 >

dotnet windows forms databinding : DataBinding to a UserControl


RobinS
12/2/2006 6:27:30 PM
Add a data source, and instead of specifying a database,
specify an Object. Choose your structure. (I've done this
with a class, not a structure, but I would think it would work.)
Then you can drag the entries from the Data Sources window
onto the textboxes to bind them.

Robin S.
---------------------------
[quoted text, click to view]

Guru
12/3/2006 1:05:48 AM
Hi,

in my .NET 2.0 application I'm using databinding for binding the data of my
data class with the controls of my main form. For the handling of my data
class I'm using a bindingSource object as member of my main form.
On my main form I'm using a usercontrol with for example 5 TextBox controls.
My data class has a property which gets and sets a struct with 5 strings.
Now I want to bind this property to the UserControl.

So what I have to implement in my UserControl to handle the binding for
filling the 5 TextBox controls and to get the data from it.
Does anyone have example code for me.

Regards

v-lliu NO[at]SPAM online.microsoft.com
12/4/2006 7:36:35 AM
Hi Ralf,

Based on my understanding, you'd like to bind some textboxes to the data
class in your program and want to achieve the data binding function of
retrieving data from the datasource to the textboxes and get the updated
values from the textboxes back to the data source. If I'm off base, please
feel free to let me know.

I have two questions here.
1. Is your data class a list/collection?

2. You have mentioned that you use a BindingSource component in your
program. Do you bind the BindingSource to the data class(if it is a list)
or a list of the data class objects(if the data class is not a list) and
then bind those textboxes to the BindingSource?

If the data class is not a list, you could use an instance of type
System.Collections.Generic.List<T> as the container of the data class
objects. You could also generate a data source based on the data class, as
Robin has suggested. Then you bind the BindingSource component to the
list/data source and then bind the textboxes to the BindingSource component.

In fact, as long as you bind a property of a control to a data source
successfully, the control's property is populated automatically by the data
source and if the user changes the value of the control's property when the
problem is run, the updated value is written back to the data source
automatically. All the above work is done by data binding architecture.

Hope this helps.
If you have anything unclear, please feel free to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Bob Powell [MVP]
12/5/2006 7:43:56 PM
You cannot expose the properties of the child elements of a user control
without some work.

One problem would be that all the Text properties of each textbox would
potentially be called "Text" and a user control can only expose one property
having a particular name.

Your user control might look something like this:

partial class myusercontrol : UserControl
{
public string TextA
{
get{return this.textbox1.Text;}
set{this.textbox1.Text=value;}
}
public string TextB
{
get{return this.textbox2.Text;}
set{this.textbox2.Text=value;}
}
public string TextC
{
get{return this.textbox3.Text;}
set{this.textbox3.Text=value;}
}
}
You could then bind your user control properties using the standard simple
binding technique.

If you wanted to be really clever you'd create a TypeDescriptionProvider for
your object that exposed child control properties as though they were
properties of the parent. I've done this myself before but as the code was
for a client I'm afraid I can't post here.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



[quoted text, click to view]

Bart Mermuys
12/5/2006 11:32:07 PM
Hi,

[quoted text, click to view]

Since you have a data _class_ with a property, you can bind this to a
property with the same type on your usercontrol.

YourUserControl.DataBindings.Add("SomeProperty", yourBindingSource,
"YourDataSourceProperty");

[quoted text, click to view]

Well, i don't think databinding actually deals very well with structures,
because structures are copied. So i don't think you can use databinding
within your UserControl to bind the TextBox Controls to the stucture
fields/props. You would need to do some manual "databinding", eg. :

public partial class UserControl1 : UserControl
{
private SomeStruct someField;

public UserControl1()
{
InitializeComponent();
}

public event EventHandler SomePropertyChanged;

public SomeStruct SomeProperty
{
get { return someField; }
set
{
if (!object.Equals(someField, value))
{
someField = value;

textBox1.Text = someField.Field1;
textBox2.Text = someField.Field2;

FireSomePropertyChanged();
}
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (someField.Field1!= textBox1.Text)
{
someField.Field1= textBox1.Text;
FireSomePropertyChanged();
}
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
if (someField.Field2!= textBox2.Text)
{
someField.Field2 = textBox2.Text;
FireSomePropertyChanged();
}
}

private void FireSomePropertyChanged()
{
if (SomePropertyChanged != null)
SomePropertyChanged(this, EventArgs.Empty);
}
}


If the data class property would return a class (subobject) instead of a
structure, then you could use databinding within your UserControl to bind
the TextBox Controls or you could then use Bob's suggestion and expose a
different property for each TextBox.


HTH,
Greetings





[quoted text, click to view]

AddThis Social Bookmark Button