share dataset between 2 forms?

  • Thread starter Thread starter Rain County
  • Start date Start date
R

Rain County

I am using VS2005 standard edition. I have a database (MSACCESS) with a
Customer table. On the main form (frmMain) of my windows application, I
have used the wizards to create a connection, a tableadapter, and a dataset.
frmMain has a datagridview for selecting a Customer (and then displaying
another gridview full of the customers orders). A button click on frmMain
will bring up a second form, frmCustomer. frmCustomer will be used to
delete or edit an existing customer or to add a new customer.

Now, the question: How can I have frmCustomer access the same dataset
(dsCustomer) as frmMain? How do I make the dataset be shared between forms?
Can I have a seperate class that contains and exposes the dataset? If so,
how do I do it? OR, is there a simple way to have the dataset be an object
of frmMain, but still be available as the datasource of frmCustomer?

Examples or links to examples would be appreciated. I am sure this is
simple, but, apparently, so am I :)

TIA

Phil
 
Rain said:
I am using VS2005 standard edition. I have a database (MSACCESS) with a
Customer table. On the main form (frmMain) of my windows application, I
have used the wizards to create a connection, a tableadapter, and a dataset.
frmMain has a datagridview for selecting a Customer (and then displaying
another gridview full of the customers orders). A button click on frmMain
will bring up a second form, frmCustomer. frmCustomer will be used to
delete or edit an existing customer or to add a new customer.

Now, the question: How can I have frmCustomer access the same dataset
(dsCustomer) as frmMain? How do I make the dataset be shared between forms?
Can I have a seperate class that contains and exposes the dataset? If so,
how do I do it? OR, is there a simple way to have the dataset be an object
of frmMain, but still be available as the datasource of frmCustomer?
--All you need to do is create a class (or module in VB.nET) and have
the dataset as a Shared/Static property. So it would work something
like this:

namespace CuckoozDemoNewsgroupStuff
{
class DataHolder
{

private static dsCustomer _customerInstance;

public static dsCustomer CustomerDataSet
{
get { return DataHolder._customerInstance; }
set { DataHolder._customerInstance = value; }
}
}
}

Now, throughout your application, you can refer to this particular
dataset via DataHolder.CustomerDataSet. So this is what you would fill
at the onset of the application and it's what you'd use, modfiy , update
etc throughout the application. In VB.NET you'd do the same thing, you
just create the properties as Shared or stick them in a module (which
btw, is just a specialized class that has all Shared properties and
methods).
Examples or links to examples would be appreciated. I am sure this is
simple, but, apparently, so am I :)

TIA

Phil
--Hopefully this gets you what you want but if you need any more help,
just let me know.

Cheers,

Bill
 
Thank you Bill,

I'll give it a try as soon as I finish the weekend chores my wife has
assigned to me.

It looks like exactly what I want.

Thanks again for the reponse...
 
Rain said:
Thank you Bill,

I'll give it a try as soon as I finish the weekend chores my wife has
assigned to me.

It looks like exactly what I want.

Thanks again for the reponse...
Let me know how it goes, if you have any problems, just drop me a line
 
Bill,

I appreciate the help you gave me previously. I have a new question that I
posted in the Windows.Forms group with no answer thus far.

Form a contains a dataGridView. In response to a menu item click, form A
creates and shows form B (from which I am giving the user the ability to
modify some display properties of the dgv, e.g. width, visible, column
heading, etc.). How can I access frmA.dgv from frmB? I thought that perhaps
declaring the dgv as Public would allow frmB to reference frmA.dgv, but it
doesn't.

I have learned to do it by passing a reference to the dgv when I call the
frmB constructor. However, I'm sure there must be a better and more general
way to share an object between forms. Is there?

Thanks, Phil
 
Back
Top