binding second form to same datasource

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
Does anyone have the complete procedure to acomplish this?
I got part of it from a book ado.net core reference from David Sceppa, but I miss the samples.

It's about having the mainform display the datagridview while using the second form to change the details.

Sofar, what I have done is add a bindingsource component to the secondform and let it point to the dataset; set it's datamember to the table associated with the datagridview.

The individual controls point to the bindingsourcecomponent with the datamember set to a specific column.

Now it get's blurry
The mainform supposed to pass in an argument to a public EditDetail method in the second form so that the data from a specific row will be shown in the controls.
In other words, they have to be in sync.

How do I do that??

thanks in adv, Richard
 
Last edited:
Erm.. same way you would pass anything else between two objects in an app; if its required for the object to work correctly, pass it as a constructor parameter.. if the object is to be created once and re-used, pass it as a property or SetXXX() method

and of sourse, the object youre passing, is the BindingSource..
 
thank you,
I finally found the original code (for anyone who is interested)
I have too see if i can make sense off it first so I can use it for my own app
It seems he uses all code

If you have any pointers for me, how this could be made easyer,
I would be glad too here it

From a theoretical point of view I need to pass the bindingsource to the method(or in this case the currencymanager wich comes down to the same thing, because you need the currencymanager to find the current row) and not use the bindingsource component because......it would create a different currencymanger?
What exactly does "pass it as a constructor parameter" mean?

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] EditDetail([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] cm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] CurrencyManager)[/SIZE]
[SIZE=2]drvDetail = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](cm.Current, DataRowView)[/SIZE]
[SIZE=2]vueDetail = drvDetail.DataView[/SIZE]
[SIZE=2]dsChapter13 = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](vueDetail.Table.DataSet, xsdChapter13)[/SIZE]
[SIZE=2]vueProducts = dsChapter13.Products.DefaultView[/SIZE]
[SIZE=2]vueProducts.Sort = [/SIZE][SIZE=2][COLOR=#a31515]"ProductName"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingContext(vueDetail).Position = cm.Position[/SIZE]
[SIZE=2]txtOrderID.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"Text"[/COLOR][/SIZE][SIZE=2], vueDetail, [/SIZE][SIZE=2][COLOR=#a31515]"OrderID"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cboProduct.DataSource = vueProducts[/SIZE]
[SIZE=2]cboProduct.DisplayMember = [/SIZE][SIZE=2][COLOR=#a31515]"ProductName"[/COLOR][/SIZE]
[SIZE=2]cboProduct.ValueMember = [/SIZE][SIZE=2][COLOR=#a31515]"ProductID"[/COLOR][/SIZE]
[SIZE=2]cboProduct.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"SelectedValue"[/COLOR][/SIZE][SIZE=2], vueDetail, [/SIZE][SIZE=2][COLOR=#a31515]"ProductID"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] b [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Binding[/SIZE]
[SIZE=2]b = txtUnitPrice.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"Text"[/COLOR][/SIZE][SIZE=2], vueDetail, [/SIZE][SIZE=2][COLOR=#a31515]"UnitPrice"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] b.Format, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] DecimalToCurrencyString[/SIZE]
[SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] b.Parse, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CurrencyStringToDecimal[/SIZE]
[SIZE=2]txtQuantity.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"Text"[/COLOR][/SIZE][SIZE=2], vueDetail, [/SIZE][SIZE=2][COLOR=#a31515]"Quantity"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]b = txtItemTotal.DataBindings.Add([/SIZE][SIZE=2][COLOR=#a31515]"Text"[/COLOR][/SIZE][SIZE=2], vueDetail, [/SIZE][SIZE=2][COLOR=#a31515]"ItemTotal"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] b.Format, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] DecimalToCurrencyString[/SIZE]
[SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] b.Parse, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CurrencyStringToDecimal[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ShowDialog = DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]cm.EndCurrentEdit()[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]cm.CancelCurrentEdit()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Last edited:
Given that youre using .NET 2.0, why make it so hard?

Each form has a bindingsource to which their controls bind, yes?
So just pass one bindingsource (connected to a filled datatable) to the other form, whereupon that other form's binding source gets its data from the current item of the given bindingsource..

VB.NET:
'Form2
Public Sub New(bs as BindingSource)
  Me.MyDatabindingSource = bs.Current
End Sub

If you say Form2.XXBindingSource.DataSource = bs, then form 2 can see all the records of form 1 and scroll them independently

For more info see the attached project
 

Attachments

  • Form2EditsForm1sRecord.zip
    30.7 KB · Views: 31
so, basicly you dragged a table from the datasources window to form1 and form2 and you filled the tableadapter in form1..

Is there anything in sceppa's code that is still usefull in the sense that it is not done automaticly in the designer already?

I guess, I still need to bind the individual textboxes like this:
drvDetail = CType(bs.Current, DataRowView)
vueDetail = drvDetail.DataView
txtUnitPrice.DataBindings.Add("Text", vueDetail, "UnitPrice")
or do I
txtQuantity.DataBindings.Add("Text", bs.Current, "Quantity")

If I where to update, would I use something like this, where bs.current is the replacement for currencymanager:
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ShowDialog = DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]bs.current.EndCurrentEdit()[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]bs.current.CancelCurrentEdit()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnCancel_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnCancel.Click[/SIZE]
[SIZE=2]DialogResult = DialogResult.Cancel[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnOK_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnOK.Click[/SIZE]
[SIZE=2]DialogResult = DialogResult.OK[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Last edited:
so, basicly you dragged a table from the datasources window to form1 and form2 and you filled the tableadapter in form1?
we dont fill tableadapters.. we use tableadapters to fill datatables :) TA = the tap, DT = the bucket :)
(i'm picky on this issue; always try to use correct names for what you mean ;))

Is there anything in sceppa's code that is still usefull in the sense that it is not done automaticly in the designer already?
ehm.. nope. Apart from, maybe, the roundtripping from decimal to currency view and back. It is interesting, but not something i would seek to do because I dont really go for the notion of converting a value from stored form to display, editiing the display value and then trying to parse it back. As an example, the datetimepicker stores a date internally and we should always use dtp.Value when getting the date. It formats that date and puts it on show, but we should never try to alter the .Text and hope that it can correctly parse our edit back into a date. We should always alter the date directly. To this end if an amount were needed to be edited I would probably either use a label to display $, then a numeric up down with 2 dp for the amount, or make a custom control that knew how to handle the editing (like dtp does)
MaskedTextBox might do it, i dont know.. never used it

I guess, I still need to bind the individual textboxes like this:
Nope. See the datasources window? You get like:

DataSetName
+-[#icon] DataTableName

The #icon is by default that of a grid. CLick the word DataTableName and a combo box appears. Change it to DETAILS and the icon changes to more like a windows form
Now drag the thing onto the form again
Instead of a Grid, many textboxes appear
You can drag individual text boxes, by expand DataTbleName then drag
Also each of these nodes can be changed from textbox, to Date/Combo/Numeric etc


If I where to update, would I use something like this, where bs.current is the replacement for currencymanager:

bs.EndEdit()
bs.CancelEdit()
 
thanks, I will try to use this information in a good way

I was busy adding a bindingsource component to the second form and set it to bs.current and then do this

txtOrderID.DataBindings.Add("Text", Me.BindingSource1, "OrderID")

I know I am not going to use it now, but whats wrong with this setup

Is this indirect passing not possible because the component works in another context or something..?
 
I was implementing your approach with my database wich consists of two related tables

Instead of just a bindingsource, I get a tableadapter also and both have arrows in the component tray

I don't know if it is relevant in any way, but the problem is:


When I show the second form, I get the same thirst row each time?
 
Last edited:
thanks, I will try to use this information in a good way

I was busy adding a bindingsource component to the second form and set it to bs.current and then do this

txtOrderID.DataBindings.Add("Text", Me.BindingSource1, "OrderID")

I know I am not going to use it now, but whats wrong with this setup

nothing is wrong with this setup, simply that the designer writes this code for you when you drop the stuff on the form
 
I was implementing your approach with my database wich consists of two related tables
Then you will need to pass both BindingSources

Instead of just a bindingsource, I get a tableadapter also and both have arrows in the component tray
The form designer doesnt know that youre going to be setting these data entities to get their data from an existing source, so it behaves as if this is all-new. Simply delete what you do not need..

I don't know if it is relevant in any way, but the problem is:


When I show the second form, I get the same thirst row each time?
The second form shows whateve ris currntly selected on the first. You told me this was what you needed. Run my example, click the middle row of the grid (14th april) note the posiiton in the box has changed to 3, meaning the .Current of form1's bind source is 14th april. Press button1, form2 appears and lets you edit the 14th of paril. Make it the 18th, close the form, presto the original is changed..
 
Then you will need to pass both BindingSources
I just need to alter the details of one table, so I don't think thats needed


I just gave you some extra information from wich startingpoint I added your
code. I did the same thing: dragged the same table to the second form in grid or in detail modus and deleted the fill method in the load_event

It only shows row nr 1..?
 
Last edited:
post some code, your project, etc like I did, as an attachement. Alternately you can edit my attached code and send it back to demonstrate the issue youre having
 
sorry, it took so lang
Computer overheated!

I have the sample ready
 

Attachments

  • Test.zip
    31.1 KB · Views: 25
Last edited by a moderator:
Back
Top