datasets on multiple forms

  • Thread starter Thread starter Grant
  • Start date Start date
G

Grant

Sorry for all the posts - Im new to c# and oop and and having a tough time
geting my head around some of this stuff. (I tell you Im surprised my
monitor has lasted this long, Ive been tempted many times to put the
keyboard through it)

I have this dataset on my main form which i need exposed to another form. I
have comboboxes that need to be bound to this dataset. I have tried creating
a new dataset on the new form and what happens is the dialog box pops up
with the option of making it typed or untyped.
the wizard has picked up the dataset from the main form and when I click OK
see the dataset created which has the same name as the dataset on the main
form.

Now when I set the datasource and displaymember on the combobox I can see
all the tables and columns and simply set these values accordingly.

When I run the app and open that form, the comboboxes are empty.

Some posts I have found say to make the dataset public and pass it into the
other form but I dont know how to do this.

Thanks again,
Grant
 
I think you hit the nail on the head when you said that you need it
"Exposed".

Use a property to expose it programatically from the MainForm:


#### Sue Doe code #######

In the main form:

public DataSet MainDataSet
{
get { return this.whateverDataSet; }
}

Now, when you create the other form, pass a reference to you main form and
keep it as a member variable. ie the constructor:

public NewForm(Form parentForm) : Windows.Forms.Form
{
this.parentForm = parentForm;
}

Now in the body of your code you can access the SAME dataSet with

myDataBoundControl.DataSource = parentForm.MainDataSet;

######### End ###########
 
Hi Grant,

If you create the other forms from your main form, you can simply pass the reference to the dataset in the constructor of your other form(s);

class MainForm()
{
DataSet ds;

SomeMethod()
{
OtherForm of = new OtherForm(ds);
of.Show();
}
}

class OtherForm
{
ComboBox comboBox1;
public OtherForm(DataSet ds)
{
InitializeComponent(); // initialize stuff

comboBox1.DataSource = ds;
comboBox1.DisplayMember = "Table1.Column1";
}
}

This will let you use the dataset in main form to fill values in comboBox1.
Note, this code assumes ds have a datatable named "Table1" and that this datatable has a column named "Column1";

Happy coding!
Morten Wennevik [C# MVP]
 
Thanks for the reply. In my new form I have the following:

public class NewAgencyForm : System.Windows.Forms.Form
- and -
public NewAgencyForm()

Where do I put this bit:
public NewForm(Form parentForm) : Windows.Forms.Form
{
this.parentForm = parentForm;
}

When I call the new form with:
NewAgencyForm frm = new NewAgencyForm()
-do I instead say:
NewAgencyForm frm = new NewAgencyForm(this)
 
Sorry, grabaged that up a bit and put the class extension stuff in the
constuctor!!

you need:

public NewAgencyForm(Form parentForm)
{
this.parentForm = parentForm;
}

and yes you cal the constuctor with:

NewAgencyForm frm = new NewAgencyForm(this)
 
I put that in and get the error that newagencyform does not contain a
definition for 'parentForm'.

I had a look on msdn for that parentForm function and tried the following:
DataSet dataSet = ((MainForm)this.ParentForm)agencyDataset

It didnt like that though. All I get is default functions like
'acceptchanges' and so on.
 
I put that in and get the error that newagencyform does not contain a
definition for 'parentForm'.

I had a look on msdn for that parentForm function and tried the following:
DataSet dataSet = ((MainForm)this.ParentForm)agencyDataset

((MainForm)this.ParentForm).agencyDataSet

You only missed a dot :)

Happy coding!
Morten Wennevik [C# MVP]
 
Nice one! That worked a bomb! Just a little slow when opening the new form
for some reason but I can live with that.

Thank you.
 
You don't need to cast it.

in the class you need a member variable for your reference to parentForm.

eg private Form parentForm;

This is what is et in the constuctor. Provided you called the DataSet
property in you main form "agencyDataSet" then the following should be fine:

DataSet dataSet = this.ParentForm.agencyDataset

If not post your code
 
Back
Top