sharing a dateset among forms

G

Guest

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

Bryan Phillips

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

Guest

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

VJ

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
 
G

Guest

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
 

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