Flip Datasources / bindingsources on the fly


M

Miro

'Sorry for the multi-newsgroup post - just really not sure which 'heading'
this relates to.

I am trying to switch the datasource/bindings to a reference on another form
during runtime. (vb.net)

Basically use the designer to create the form, let it add the dataset and
databindings to the form as it build it.
Then on sub new of the form2 switch the reference to the bindingsource and
dataset to the ones passed in from form1, and all
other controls should continue to work as is - since they all reference the
same fields from the dataset, just that the dataset and the
binding source points somewhere else now.

It did not work - the data shown in the datagrid is not the proper data.

(hope that makes sense)

Here is what I did:

I created 2 forms,
form2 on the Public Sub New() function I created some new parameters.

Form2:
Public Sub New( ByVal _xMyForm1BindingSource As BindingSource, ByVal
_xMyForm1Dataset As MyDataSet)

So from form1, i call form2 like this:
Dim fForm2 As New frmForm2( Me.FKBindingSource, Me.MyDataSetDS)
fForm2.ShowDialog()

So far so good.

Now I put a datagrid on form2, added this to the top of form2 so i can have
access to the passed in variables.

Private _MyDataSet As MyDataSet
Private _MyForm1BS As BindingSource

and within the SubNew of form2 i did this:
Form2:
Public Sub New( ByVal _xMyForm1BindingSource As BindingSource, ByVal
_xMyForm1Dataset As MyDataSet)

'Setting up references to the dataset and bindingsource ??
Me._MyDataSet = _xMyForm1Dataset
Me._MyForm1BS = _xMyForm1BindingSource

'Here is where the issue starts ****
'If i do this - this works
'myDataGridView is my datagridview on the screen
Me.myDataGridView.DataSource = Me._MyForm1BS 'This shows the proper data
in the datagridview from form1

'But ....if i rem out the line above, and go to the form2, forms editor,
click on the datagridview ( myDataGridView ) and then
'select a datasource, let it pop a dataset, bindingsource, and table adapter
to the form.
'Now here on the form I can use the gui wizards to setup my columns and
such...
then replacing the above line I was hoping to add references to the same
bindingsource ( which has the same tables )...i was under
the impression that the switch would be flawless and now the forms dataset
and bindingsource point to the dataset and binding source on form1.

'This does not work
'Me.MyNewForm2Dataset = Me._MyDataSet
'Me.MyNewForm2BindingSource = Me._MyForm1BS
'I was hoping this would set reference to my dataset and databinding sent
in, so it actaully points to the databindings from form1 during runtime, but
during design time, i have the full wizards at my disposal.

'It compiles and runs, but the data displaying in the datagridview is not
the data that displays if I code it manually.

EndSub

Is this even possible what I am attempting?

Thanks,

Miro
 
Ad

Advertisements

C

Cor Ligthert[MVP]

Miro,

The main thing is that you should make the datasource/bindings friend
instead of private, private means that it is only visible on your Form1,
Friends means that it is visible in the complete application. (Public means
that it is visible from everywhere)

Cor
 
M

Miro

Form 1 passes the 'datasource/bindings' in on form2:sub new

then i just assign it as a private variable on top.

I can make it 'friend' but i still don't fully understand why the variable
switch to reference is not working.

The private variables are ontop of form2 where form1 passed in its
bindingsource/dataset. I was setting it up like a 'property' originally.


Miro
 
M

Miro

I have stumbled upon some code but still havnt gotten it to work

'Me.MyBindingSource.ResetBindings(False)
'Me.MyBindingSource.ResetCurrentItem()

Its like when I use the wizard to create the form, and let the controls
point to the bindingsource they put on the form, during runtime, when I try
to change the reference of that bindingsource to point to a binding source I
passed in, it doesnt refresh the controls. Its like the controls still
point to the original binding source somehow.

There has got to be a simple way of just saying ( within the code )
myCurrentBindingSource = BindingSourcePassedIntoForm

and any control linked to myCurrentBindingSource automatically is pointing
to the new one.
 
M

Miro

I beleive I got it...

instead of trying to set the bindingsource to the 'binding source' passed in
so i dont have to flutter thru all the controls and reset the binding
source....

I passed in my parent binding source, and set my forms bindingsource to
reference the parents.

-Still would have been nice to set the current binding source to a reference
of itself on another form instead of passing the parent binding source its
linked to and the table releation.

Thanks,

Miro
 
C

Cor Ligthert[MVP]

Miro,

I am not sure that I understand your problem, however remember that you pass
in Net seldom really something.
You only pass references from objects, it still are the same objects.

Cor
 
Ad

Advertisements

M

Miro

i was trying to change my bindingsource on my main form.
I will try to type out a mini example of what I was trying to do so you can
follow along.
If not, let me know, I will create a dummy solution if you want to see it.

Here is an example (pseudocode)

You have a 1 to many relationship on a file with two tables.
FileParent -> FileChildren

Create 2 forms... FormOne, FormTwo

-We will use the wizard in this case to speed things up.

Drag and drop a datagrid of FileParent on FormOne.
For test example, add the FileChildren datagrid to the Form1, and link the
binding source so you get the relationship of DataGrid2 to DataGrid1. -That
way you should see the relational data as you click around in DataGrid1

Goto FormTwo, drag and drop a datagrid of FileChildren on FormTwo.

Now, by dragging the datagrid onto FormTwo, it added a dataset,
bindingsource, tableadapter to FormTwo.
-No problem we will take care of this at runtime - but this allows us to use
all the nice graphical wizards to create the datagridview and such.

Ok,
Now go to FormTwo Sub New(
and add this:
FormTwo Sub New( byval FileParentBindingSource as bindingsource, byval
FileChildrenBindingSource as bindingsource )

'Now, Lets say the wizard added onto Form2 the bindingsource with a name of
ChildrenWizardBS
'So I would assume, by doing this
Me.ChildrenWizardBS = FileChildrenBindingSource
'would change the wizardbs to actually be the FileChildrenBindingSource from
FormOne that was passed in.
'Thus, makign all the fields ( in this case its just the datagrid, but if i
had textboxes and everything ) now point to the relational bindingsource
from FormOne. ( instead of looping thru all the controls and reseting the
binding source - go to the source ).

'That does not work for some reason however.

'But this does:
Me.ChildrenWizardBS.DataSource = FileParentBindingSource
Me.ChildrenWizardBS.DataMember = "FileRelationName"

EndSub

Dont forget now to go to form1, put a button on the form, and on the button,
call form two like this:
Dim fNewForm as New frmFormTwo( FileParentBindingSource,
FileChildrenBindingSource )
fNewForm.ShowModal()

I was trying to set the FormTwo bindingsource to point to the bindingsource
of FormOne. But it wouldn't seem to work.
I had to manually set the DataSource and DataMember to get it to work, and
pass in the 'ParentBindingSource' to the form, instead of
passing in the 'Child BindingSource'

Let me know if that makes sense.

Miro
 
J

Jack Jackson

i was trying to change my bindingsource on my main form.
I will try to type out a mini example of what I was trying to do so you can
follow along.
If not, let me know, I will create a dummy solution if you want to see it.

Here is an example (pseudocode)

You have a 1 to many relationship on a file with two tables.
FileParent -> FileChildren

Create 2 forms... FormOne, FormTwo

-We will use the wizard in this case to speed things up.

Drag and drop a datagrid of FileParent on FormOne.
For test example, add the FileChildren datagrid to the Form1, and link the
binding source so you get the relationship of DataGrid2 to DataGrid1. -That
way you should see the relational data as you click around in DataGrid1

Goto FormTwo, drag and drop a datagrid of FileChildren on FormTwo.

Now, by dragging the datagrid onto FormTwo, it added a dataset,
bindingsource, tableadapter to FormTwo.
-No problem we will take care of this at runtime - but this allows us to use
all the nice graphical wizards to create the datagridview and such.

Ok,
Now go to FormTwo Sub New(
and add this:
FormTwo Sub New( byval FileParentBindingSource as bindingsource, byval
FileChildrenBindingSource as bindingsource )

'Now, Lets say the wizard added onto Form2 the bindingsource with a name of
ChildrenWizardBS
'So I would assume, by doing this
Me.ChildrenWizardBS = FileChildrenBindingSource
'would change the wizardbs to actually be the FileChildrenBindingSource from
FormOne that was passed in.

Form2 has a form variable named ChildrenWizardBS that contains a
reference to the BindingSource that the wizard set up. When you
change the contents of that variable, you don't really change
anything. The old BindingSource still exists, and the DataGridView
still points to it.

You need to point the DataGridView to the new BindingSource:

Form2DataGridView.DataSource = me.ChildrenWizardBS

What you have done is essentially this:

Dim bs1 as BindingSource = somebindingsource
Me.MyDataGridView.DataSource = bs1
bs1 = someotherbindingsource

Changing bs1 has no effect on MyDataGridView's DataSource.
 
Ad

Advertisements

M

Miro

Nuts!

I am ok with passing in my 'parent binding source' and resetting the
datasource and the datamember on the form load.
That way i still dont have to fly though every control and reset the binding
to the new binding I have passed in. I only have
to do it in one spot.

I was hoping I could somehow change the "reference" to the 'form2
bindingsource' to point to the new one given.
I was even trying to play with 'generatemember = false' and things like
that.

Thank you guys for your help,
It made sense in "theory" of what I was trying to do. :)

Thanks again,

Miro
 

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