Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DataGridView is not updating in master/detail form

17 views
Skip to first unread message

Jay

unread,
Nov 21, 2009, 8:17:37 PM11/21/09
to
I have two tables with the following info below:

Items Table
ItemID - Primary Key - Autonumber
ItemCode

[Item Price] Table
ItemPriceID - Primary Key - Autonumber
ItemID
Price

Items table is the parent form in my master/detail form. Item Price is the
detail form using DataGridView.

Now when I add new record the data in datagridview does not update. But
there is no problem adding a new record in the datagridview if there is
already a record in the parent form.

Another scenario is if I will not use the foreign key constraint and after
adding a new record both in parent and child form the child form has a value
of -1 in the ItemID. But the ItemID in the Items table is incrementing
correctly based on the next value of the last ItemID.

Anyone know how to solve this?

Cor Ligthert[MVP]

unread,
Nov 22, 2009, 2:54:21 AM11/22/09
to
I know how to solve it, but I never made or found a nice sample.

The point is that you should do, as there is a parent child relation, the
creating of new always in a certain sequence.

First all the new parent rows and after that the new detail rows.

When you do the delete in the opposite sequence.

There is a cascade property in the database, but I never saw this doing a
job for me in combination with AdoNet.

To get the new rows you can use the rowstate property of the datarows.

With only updates this problem does not exist.

Cor

"Jay" <jpa...@gmail.com> wrote in message
news:#C3qVGxa...@TK2MSFTNGP06.phx.gbl...

Jay

unread,
Nov 23, 2009, 5:29:21 AM11/23/09
to
Hi,

Thanks for the reply. But I don't get what you mean. I also tried the
cascade update and delete both in the database and in the vb.net dataset
designer.

I did try to delete both the tables and add it again to the dataset but
nothing happens.

"Cor Ligthert[MVP]" <Notmyfi...@planet.nl> wrote in message
news:u7OXDk0a...@TK2MSFTNGP04.phx.gbl...

Rich P

unread,
Nov 23, 2009, 12:02:52 PM11/23/09
to
Without knowing the guts of your app it is difficult to say what is
going on.

Here is one Master/Detail scenario that I use. This may or may not be
related to your situation.

I add a Dataset file (.xsd file from the AddNew Items list) and I add
.Net tables to this Dataset (right click in the Dataset window and
select Add New Table). Here you can add constraints (note: you can do
this all in code also - but the .xsd gives you a graphical user
interface so you can see what you are doing - and this gets persisted to
the disk - but not any data - just the table constructs). When you add
your master/detail constraint (relationship - foreign key constraint) --
depending on your constraint - the standard one is that you can't add
detail records to the detail table without first adding a parent record
(the purpose of this constraint is to prevent orphan detail records).
This would be like a "one to many" relationship. First you add a record
to the (local) master table, then you can add a detail record to the
(local) detail table. Then in your datagridview control you would
display the contents of your detail table. Also, this data is all
contained within the app itself (in memory). If you need to persist
this data -- you have to write the code to write it to your sql server
database tables.

Also, unlike an Access mdb app where Access automatically will add a
connection between a Master/Detail form(s) you have to write your own
code for this (.Net is not an integrated development environment like
Access - you are a little bit more on your own in .Net). This means
that if you display a master record on your form and you want to display
the corresponding detail record(s) in the datagridview - you have to do
something like this:

Datagridview1.Datasource = dataset1.Tables("Detail").Select("ID = " &
txtID.Text)

This would filter your detail table to only display the records related
to the selected ID from the Master table. If you have a MoveNext button
on your form, you select the record from the (local) master table to
display on your form and in the same code you reset the datasource of
your datagridview as stated above.

Rich

*** Sent via Developersdex http://www.developersdex.com ***

0 new messages