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:
the zip file is missing the Database1.mdf file

additionally, remove the BIN and OBJ folders before you make the zip
 
ok, I included all
Thought I had to remove the mdf because yours diddn't have one also
My mistake
 

Attachments

  • Test.zip
    230.1 KB · Views: 27
Last edited:
Youre going to kick yourself..

Form1's last name view is bound to something called FKLastNamesNamesBindingSource - so you were passing another bind source called LastNamesBindingSource - whose position is always 0, because it is never changed - LastNamesBindSource.Current always points to "1 / tree"

Change:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form2[/SIZE]
[SIZE=2]f = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form2([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].FKLastNamesNamesBindingSource)[/SIZE]
[SIZE=2]f.Show()[/SIZE]
 
I would but I give myself a break this time!

the thing to do is bind the foreign key bindingsource, wich actually is listed as a datamember of the original namesbindingsource to the new bindingsource(or pass it to the other bindingsource)

So, if you have a datamember, that would be the one to pass

I can live with that!

another issue is, if I change anything now in the gridview on the second form, I get a big error
What update logic do I need in this case as oppose to your sample (see post 12)?
oh, excuse me
It seems the datagrid doesn't like editing the same string
If typed all at once, there is no problem
Is there a setting or a workaround for this?
 
Last edited:
I take it you are talking about the MS Press Book ADO.NET by David Sceppa?

That is for .net 1.1 and your Primary Platform shows .net 2.0 - what the book tells you won't work in VS 2005.

I know exactly what chapter you are talking about because that's the book I self taught on.

I would suggest using Cjard's examples or searching the web for .net 2.0 examples, because the book will just confuse the matter if you try resourcing from it!
 
I take it you are talking about the MS Press Book ADO.NET by David Sceppa?
Yes, but I have the 2005 edition titled: ADO.NET 2.0
seems the new book contains older stuff too, where there are better solutions available
But it's not completely useless, I hope
 
ah thats good then lol, thought you were using the first version :) no offense but known people to have done that!!

I'll check one of my apps, I'm sure I've got a 2nd form binding to a row like that.

If not, another way would be to save the present row as a variable using it's ID, then on the other form using a query to load the data based on this ID.

I know in most cases I removed the "EditDetail" sub part, and instead of calling a model form, I used a series of panels which were bound to the dataGrid row and made these visible as and when needed....
 
Last edited:
thanks
I could always use some more insight
I know in most cases I removed the "EditDetail" sub part,
Sceppa's approach with the currencymanager
seems indeed to much of a good thing as shown in the posts above from Cjard

Momentaraley, I am looking for a way to avoid the error message when a value is edited in a single cell
Can you give me a hint in that direction?
 
I've sent a note to the forum administrator as for some reason I cannot see any of cjard's posts, so I'm not sure what he has wrote here!!!!

I wasn't too keen on the way the currencymanager way worked - it was great in the sense it was a quick and easy way to edit a row in the grid without having to edit the grid itself (personally I hate having to change data in a grid itself - to me a grid should be for viewing only), but then it led to issues (in .net 1.1) where the final column I edited wasn't saved, and on the "submit" event I had to remember to put focus on another field before updating so that it would all get saved!!!!!

I just looked at the start of the thread and saw the code you posted - very very similar to what I used in .net 1.1

Checked my app, and I've scraped using the currency manager. Like I thought instead I use a panel that has textboxes / comboboxes bound to the same dataTable as the dataGrid, so any changes made to the panel affects the grid. I use the grid to view the data and then a panel to edit the data.

All I do when I edit the data, is hide the tab control (which the grid is part of), and show the panel. when the data is updated, the panel hides and the tab control shows again.

I found that way much easier, but as they say "each to their own" :D
 
hmm.. have to think about that

anyway, the thing Cjard instructed was to pass the bindingsource to the form2 constructor

In short:
VB.NET:
dim f as form2
f=new form2(table1bindingsource) 
f.show

::second form
public sub new(byval bs as bindingsource)
table1bindingsource=bs.current
end sub

I was not planning to use the grid also, but I can change this to a details view
Guess, I have to play with it for a while till I get the update thing completely the way I want
 
If I edit the name, I get no error

If I edit the ID to a wild number, i get the expected error about missing parent record. Remeber, youre editing a child record now. YOU CANNOT EDIT A CHILD ID TO BE SUCH THAT IT HAS NO MATCHING PARENT ID - doing so orphans the data by breaking the relationship. Dont do it. make that column read only and dont mess with it. If you nee dto edit the ID, edit the parent and the child id will cascade update, or edit the child ID it to a parent ID that exists (adoption, not orphaning)
 
Could you perhaps supply me with some code to update to the database
because I don't get it yet.

As I understand the wizard is useless now that I have related tables
The automatic update will not work.

From msdn I have a sample that will create a customAdapter with some insert, add, and Update commands created, but it's still too narrow. It doesn't consider relational data.
Also, do I have to rearrange my whole way of thinking, because if I create a new adapter I can not use anything that is automaticly created when I drag the table out off the datasources window? I loose control off the navigatorcontrol and what about the other table that's in details modus. Do I have to do a lot manually now, as in typing?

VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Data.SqlClient[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] FDataBase[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TDB.mdf;Integrated Security=True;User Instance=True"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sSQLSelect1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Select * from Orders"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sSQLSelect2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Select * from Customers"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(sSQLSelect1, cs)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(sSQLSelect2, cs)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myDataSet = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet()[/SIZE]
 
'or in one adapter
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ss [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"SELECT * FROM Customers; SELECT * FROM Orders"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(ss, cs) [/SIZE][SIZE=2][COLOR=#008000]'cs is my connectionstring
[/COLOR][/SIZE] 
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] FormMain_Load([/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][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
 
[SIZE=2]da1.Fill(myDataSet, [/SIZE][SIZE=2][COLOR=#a31515]"Orders"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]da2.Fill(myDataSet, [/SIZE][SIZE=2][COLOR=#a31515]"Customers"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]'Just the gridview[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].JobsDataGridView.DataSource = myDataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].JobsDataGridView.DataMember = [/SIZE][SIZE=2][COLOR=#a31515]"Orders"[/COLOR][/SIZE][/SIZE]


This is confusing, to say the least

I have to find the other code again, but it's on my other computer.
I hope you know what I mean?
 
Last edited:
Could you perhaps supply me with some code to update to the database
because I don't get it yet.

As I understand the wizard is useless now that I have related tables
The automatic update will not work.
Hmm.. I never had a problem with related tables.. nothing is done differently, but you MUST remember that if you have an FK enforcing your relationship, that parent records must be created first. The following code is an example:
VB.NET:
     Private Sub NamesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ParentBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ParentBindingSource.EndEdit()
        Me.ParentTableAdapter.Update(Me.MyDataSet.Parent)


        Me.ChildBindingSource.EndEdit()
        Me.ChildTableAdapter.Update(Me.MyDataSet.Child)

    End Sub

From msdn I have a sample that will create a customAdapter with some insert, add, and Update commands created, but it's still too narrow. It doesn't consider relational data.
No, it wont.. the data access layer doesnt care about your relationships - all a relationship does if filter and show children of the currently shown parent. You must still:

Fill the parent data
Fill the child data

Save the parent data
Save the child data

yourself

Also, do I have to rearrange my whole way of thinking, because if I create a new adapter I can not use anything that is automaticly created when I drag the table out off the datasources window? I loose control off the navigatorcontrol and what about the other table that's in details modus. Do I have to do a lot manually now, as in typing?

Um.. not really. You dont really say what youre having trouble with, but basically, ref my paragraph above, about fill parent, fill child, edit either, save parent, save child - thats the order you do it in, and you ahve to do that yourself. The code you msut do isnt exactly a lot.

Read the DW2 link about displaying related data, and saving related data

VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Data.SqlClient[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] FDataBase[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TDB.mdf;Integrated Security=True;User Instance=True"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sSQLSelect1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Select * from Orders"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sSQLSelect2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"Select * from Customers"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(sSQLSelect1, cs)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(sSQLSelect2, cs)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myDataSet = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet()[/SIZE]
 
'or in one adapter
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ss [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"SELECT * FROM Customers; SELECT * FROM Orders"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(ss, cs) [/SIZE][SIZE=2][COLOR=#008000]'cs is my connectionstring[/COLOR][/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] FormMain_Load([/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][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
 
[SIZE=2]da1.Fill(myDataSet, [/SIZE][SIZE=2][COLOR=#a31515]"Orders"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]da2.Fill(myDataSet, [/SIZE][SIZE=2][COLOR=#a31515]"Customers"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]'Just the gridview[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].JobsDataGridView.DataSource = myDataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].JobsDataGridView.DataMember = [/SIZE][SIZE=2][COLOR=#a31515]"Orders"[/COLOR][/SIZE][/SIZE]

No, no, no.. you've done all this already; you have a tableadapter with this code in. You dont ned to duplicate it here, all you do is call the Update() command of the table adapter, passing a datatable with modified rows, and the changes are sent back to the database.

Whatever tutorial you got this out of, throw it away; its 2 years out of date at least
 
I read a lot of tutorials, and a lot of the solutions in regards to relational data invalves creating custom sqlcommands or commandbuilder or something like that. They can't all be out of date, are they.
I just google "update relational data ado.net" if I can't find it here.

I started looking because when I wanted to save to the database this diddn't work
VB.NET:
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] If Validate() Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000] Me.CustomersBindingSource.EndEdit()
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]Me.OrdersBindingSource.EndEdit()
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]Remove rows childtable
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]Me.OrdersTableAdapter.Update(Me.myDataSet.Orders.Select("", "", DataViewRowState.Deleted))
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Handle ; Add, Update, en Delete in  parent table[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Me.CustomersTableAdapter.Update(Me.myDataSet.Customers.Select("", "", DataViewRowState.Added Or _
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000] DataViewRowState.ModifiedCurrent Or _
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000] DataViewRowState.Deleted))
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Handle; Add, Update, en Delete in childtable
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]Me.OrdersTableAdapter.Update(Me.myDataSet.Customers.Select("", "", DataViewRowState.Added Or _
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]DataViewRowState.ModifiedCurrent Or _
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000] DataViewRowState.Deleted))[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]end if
[/COLOR]
[/COLOR][/SIZE]
 
still nothing, if I press the debug button, it loads the data - then I make changes, press the savebutton and reload the form again - I get the same data back without modifications?
You tell me, because I can not see the solution quickly without possibly getting into redundant solutions that I see all over the place.

People should throw away their outoffdate stuff!
 
Back
Top