Dataset question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to create a dataset in one form of a Win app and use that same
dataset in a second form of the same app? If so, how? Thanks.
 
Joseph said:
Is it possible to create a dataset in one form of a Win app and use that same
dataset in a second form of the same app? If so, how? Thanks.

What is the relationship between the two forms? For example, does the
first form create and show the second form as a dialog? Can the user
open them independent of one another? Or perhaps they have a "wizard"
relationship: the first form leads to the second form? ... ?
 
Hello Joseph,

There can be two approaches

1. Create a public variable in the form, access it like myForm.myVariable.
This will be available as long as the form is alive.
2. Create a global variable.

Regards
Cyril Gupta

You can do anything with a little bit of 'magination.
I have been programming so long, that my brain is now soft-ware.
http://www.cyrilgupta.com/blog
 
Cyril said:
Hello Joseph,

There can be two approaches

1. Create a public variable in the form, access it like myForm.myVariable.
This will be available as long as the form is alive.

Well, I would make it a property. Regardless, all this does is move the
problem up one level: now there is the problem of how to get a
reference to the form in question in order to call the property getter.
That, in turn, depends upon the relationship between the forms.
2. Create a global variable.

This is the solution of last resort, mostly because it precludes having
more than one copy of the form. Maybe that is a constraint of the
program, but without knowing more about how the forms are called and
what they do, that's impossible to say.
 
Put your dataset in class library and share it could be easier.

chanmm
 
The relationship between the forms is such that the first form shows the
second as a dialog.
 
The easiest way to do this, then, is to create a DataSet property on
your second form, and have your first form set it:

public class SecondForm : Form
{
private DataSet _myDataSet;

public SecondForm() { this._myDataSet = null; }

public DataSet MyDataSet
{
get { return this._myDataSet; }
set { this._myDataSet = value; }
}
}

public class FirstForm : Form
{
...
DataSet ds = new DataSet();
... populate the data set ...
SecondForm dialogForm = new SecondForm();
dialogForm.MyDataSet = ds;
if (dialogForm.ShowDialog() == DialogResult.OK)
{
... do something with results ...
}
dialogForm.Dispose();
}

Since the property accepts a reference to a data set, the second form
will be manipulating / reading the same data set that is held by the
first form, so there is no need to assign it back or "get" it from the
second form after it's done. When the dialog returns the data set held
by the first form will have been changed (if the dialog is meant to
change it).
 

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

Back
Top