Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

Tabs, Bound Controls & UserControls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian P. Hammer - 25 Jul 2006 21:53 GMT
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
Linda Liu [MSFT] - 26 Jul 2006 04:06 GMT
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.
Brian P. Hammer - 26 Jul 2006 15:16 GMT
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

> Hi Brian,
>
[quoted text clipped - 110 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Linda Liu [MSFT] - 31 Jul 2006 06:30 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.