Link to home
Start Free TrialLog in
Avatar of Arikael
ArikaelFlag for Switzerland

asked on

Open a Detail Form and passing a DataSet to it

Hi experts

I have a DataGridView and when I double click on a row a new detailform gets opened. So far no problem.
Because I don't want to load my data twice I pass my dataset as a parameter to my detailform (see code).

I already have defined a DataSet (MyDataSet) and multiple BindingSources in the designer, so they get initialized in InitializeComponent() (generated by VS2008)
All the bindingsources have this.MyDataSet as DataSource.

I now do the following
this.MyDataSet = (MyDataSet)DataSet;

this.MyDataSet now holds all the data from DataSet (this works)
But I have to re-set all DataSources of the BindingSources and I can't explain why, because I just assign the same variable as the Designer does.

I know it's probably not the best way to do it and when you know a better one please tell me.

Hope it's clear what I mean :)

Thanks for your help
public DetailForm(int DetailId, DataSet DataSet)
            : base(DetailId, DataSet)
        {
           
            InitializeComponent();
            this.MyDataSet = (MyDataSet)DataSet;
            
//why do I have to do this?
this.articlesBindingSource.DataSource = this.MyDataSet;
            this.DetailBindingNavigator.BindingSource =this.articlesBindingSource;
            this.weightCategoriesBindingSource.DataSource = this.MyDataSet;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JayeshKitukale
JayeshKitukale

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arikael

ASKER

This makes sense :-)

I'm binding the datasource in the designer, so I can bind my controls to the the bindingsource (respectively the fields in the DataTable behind the DataSource of the BindingSource)

Do you know a better way?
Avatar of JayeshKitukale
JayeshKitukale

You can avoid the design time binding and go with the code-only way (this is the expert's way). Relying on the designer is a quick way but does not scale up with advanced design and implementation scenarios (typical problem with wizards and designers).
Avatar of Arikael

ASKER

Thanks for your help