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 / March 2007

Tip: Looking for answers? Try searching our database.

sharing a dateset among forms

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Flavio Raimondi - 13 Mar 2007 19:53 GMT
Hi all,

I render different views of the same data using different forms (coincise
view, detailed view and so forth...), and I would like to avoid to fill in
the table adapters going back and forth among views.
The forms are built using standard components (dataset, datagrid,
bindingSource, bindingNavigator...).
The problem is  that every time you modify the form the designer regenerates
the form.InitializeComponent, allocating new objects for the bindingsource,
dataset, etc.
So how can I share a dataset among different windows forms, avoiding to
manage by hand the InitializeComponent ?

Any suggestion would be appreciated.
Bryan Phillips - 13 Mar 2007 23:00 GMT
Create a static variable in a class somewhere to contain the dataset and
populate it before your main form loads.  Then, in each form's load
event, set that form's dataset equal to the static dataset.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

> Hi all,
>
[quoted text clipped - 10 lines]
>
> Any suggestion would be appreciated.
Flavio Raimondi - 15 Mar 2007 10:39 GMT
Thank you for your suggestion, but sorry it doesn’t work out.
The only place I have been able to assign to the form's dataset,
bindingsource and tableadapter a static pre-initialised ones and seeing the
shared data is in the InitializeComponent() of the "#region Windows Form
Designer generated code", maybe because there their .BeginInit() happen.
Because this section is automatically generated by the designer, if you put
the assignments there you lose them everytime you touch the graphical aspects
of the form.
My question was: how to do that in a way that is "compatible" with the IDE?
Using the form_load event handler seems to me not to work.

> Create a static variable in a class somewhere to contain the dataset and
> populate it before your main form loads.  Then, in each form's load
[quoted text clipped - 19 lines]
> >
> > Any suggestion would be appreciated.
VJ - 15 Mar 2007 14:56 GMT
You could do it in the constructor after initialize component, but that
maybe not a good idea depending the volume of data. What you need is
application level static variable that you initialize when your application
loads. If you fetch data at a later stage after application loads, you can
assign it to the variable, and the update is available to every other class.

How to do this is, create a static class with static variable and put in
same namespace as the other class, so its available to access from all other
classes.

A little more detail explanation of what Bryan had given..!, What Bryan
suggested will work also, not sure what is breaking for you. Can you give a
outline of code of what you are trying?

VJ

> Thank you for your suggestion, but sorry it doesn't work out.
> The only place I have been able to assign to the form's dataset,
[quoted text clipped - 38 lines]
>> >
>> > Any suggestion would be appreciated.
Flavio Raimondi - 17 Mar 2007 10:49 GMT
Thank you for your support, but there is something in my code that breaks.
Here is an outline of code of what I am trying:

1) context:
- simple designer generated forms app.
- each form contains a DataGridView bound to a BindingSource that is bound
to a DataSet & Table, plus a BindingNavigator bound to the same BindingSource

2) creation of static dataset
In the project namespace, I added to the static class Program:
namespace GesDent
{
   static class Program
   {
       public static DataSetGesDent dataSetGesDent;
       public static
GesDent.DataSetGesDentTableAdapters.tblPatientsTableAdapter
tblPatientsTableAdapter;
       public static System.Windows.Forms.BindingSource
tblPatientsBindingSource;

       protected static void LoadDB()
       {
           dataSetGesDent = new DataSetGesDent();

           tblPatientsTableAdapter = new
GesDent.DataSetGesDentTableAdapters.tblPatientsTableAdapter();
           tblPatientsBindingSource = new
System.Windows.Forms.BindingSource();
           tblPatientsBindingSource.DataSource = tblPatientsTableAdapter;
           tblPatientsTableAdapter.ClearBeforeFill = true;
           tblPatientsTableAdapter.Fill(Program.dataSetGesDent.tblPatients);
       }

       public static void SaveDB()
       {
           tblPatientsBindingSource.EndEdit();
           
tblPatientsTableAdapter.Update(Program.dataSetGesDent.tblPatients);
       }

And then, in the Main function, I put a call to the LoadDb method:
       static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           LoadDB();
           Application.Run(new FormPatients());
       }

3) use of the data
a) If I put into the form’s constructor the static dataset stuff assignment
to the local dataset instance after InitializeComponent call, as you told me,
I cannot see any data in the grid:
namespace GesDent
{
   public partial class FormPatients : Form
   {
       public FormPatients()
       {
           InitializeComponent();
           this.dataSetGesDent = Program.dataSetGesDent;
           this.tblPatientsTableAdapter = Program.tblPatientsTableAdapter;
           this.tblPatientsBindingSource = Program.tblPatientsBindingSource;
       }

b) if I make the assignment into the designer generated InitializeComponent
method, just after the allocation code (that I have to leave, otherwise the
designer doesn’t recognize the form any more), everything will work:
       private void InitializeComponent()
       {
           this.components = new System.ComponentModel.Container();
           System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(FormPatients));
           this.dataSetGesDent = new GesDent.DataSetGesDent();
           this.tblPatientsTableAdapter = new
GesDent.DataSetGesDentTableAdapters.tblPatientsTableAdapter();
           this.tblPatientsBindingSource = new
System.Windows.Forms.BindingSource(this.components);
           // section added by hand
           this.dataSetGesDent = Program.dataSetGesDent;
           this.tblPatientsTableAdapter = Program.tblPatientsTableAdapter;
           this.tblPatientsBindingSource = Program.tblPatientsBindingSource;
           // end section added by hand
           this.dataGridViewPatients = new
System.Windows.Forms.DataGridView();
        …..
What I am missing?
Thank you in advance for your help.

Flavio

> You could do it in the constructor after initialize component, but that
> maybe not a good idea depending the volume of data. What you need is
[quoted text clipped - 54 lines]
> >> >
> >> > Any suggestion would be appreciated.

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.