Tabs, Bound Controls & UserControls

B

Brian P. Hammer

All - I have a form with a tab control. There are 9 tab pages with various
data bound controls on each. I was thinking of moving theses to seperate
usercontrols and then adding the control to each tab page.

I haven't conceptually grasped how to handle the data part though. Should I
pass a dataset to each user control or set up the dataset on each control.
Any ideas on which way to proceed with an example maybe... would be great.

Thanks,
Brian
 
L

Linda Liu [MSFT]

Hi Brian,

Thank you for posting.

Your thinking of moving those controls on tab pages to seperate
UserControls is very good.

There're several options for you on how to handle the data part. As you
said, you could set up the dataset on each UserControl or pass a dataset to
each UserControl.

For the first option, the biggest disadvantage is that you must fill the
dataset in each UserControl which will result in extra code.

So I think you should adopt the second option. There're still two ways to
do this. I will explain the two ways with one UserControl for instance in
the following part. The samples below require that you have created a
UserControl named UserControl1 in the project and dragged&dropped a textbox
named textBox1 onto UserControl1.

1. Bind the controls to the DataSet in the UserControl's constructor
Below is a sample code snippet.

public partial class UserControl1 : UserControl
{
private DataSet dataset = new DataSet();

public void SetDataSet(DataSet _DataSet)
{
this.dataset.Clear();
this.dataset.Merge( _DataSet);
}
public UserControl1()
{
InitializeComponent();
this.textBox1.DataBindings.Add(new Binding("Text", dataset,
"TableName.FieldName"));
}

The disadvantage of this way is that if the data in the controls is
modified by user, the changes won't reflect in the dataset in the form.

2. Bind the controls to the DataSet in a method
The following is a sample code snippet.

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}

public void SetDataSet(DataSet dataset)
{
// if textBox1 have been bound, remove the binding first
if (textBox1.DataBindings.Count > 0)
{
this.textBox1.DataBindings.RemoveAt(0);
}
// bind textBox1 to dataset
this.textBox1.DataBindings.Add(new Binding("Text", dataset,
"TableName.FieldName"));
}
}

This way doesn't have the disadvantage of the previous way. Any changes in
the data bound controls will be reflect in the dataset in the form. I think
it is the best way to pass dataset to UserControl.

For the above two ways, the code of passing dataset to UserControl in the
form is the same. The sample code requires that you have dragged&dropped
UserControl1 named userControl11 onto one of the tab pages in the form.

private void Form1_Load(object sender, EventArgs e)
{
// fill the dataset in the form
DataSet1TableAdapters.StateTableAdapter tableAdapter = new
DataSet1TableAdapters.StateTableAdapter();
tableAdapter.Fill(dataset.TableName);
// pass the dataset to userControl1. The same to other
usercontrols.
this.userControl11.SetDataSet(dataset);
}

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.
 
B

Brian P. Hammer

Thanks Linda. Looks like option #2 is the way to go. I'll let you know if
I come up with any questions.

Regards,
Brian
 
L

Linda Liu [MSFT]

Hi Brian,

How is it going with your solving this problem?

If you have any questions, please feel free to post in the newsgroup and we
will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top