datagridview does not update in master detail relation

D

Dirk W

I setup a simple database with a master and a detail table. Both are
connected via a datarelation. Data of each table is shown in a
datagridview (dgLektion and dgLektionlernkarten). When I start the app
for the first time, everything works fine: all master rows are shown
in dgLektion and the appropriate child rows of the first selected
master row are shown in dgLektionlernkarten.

But if I click e.g. on the 2nd row of the master table,
dgLektionlernkarten is not updated and therefore still shows the child
rows of the previous selected master row.

Even more strange for me is that if I click on the column header in
dgLektion and so change the order of the master rows, than
dgLektionlernkarten is updating correctly and showing the child rows
for the now topmost master row.

Can anyone give me a hint what I am doing wrong?

Code:

SqlCommand selCommandLektion = conn.CreateCommand();
selCommandLektion.CommandText = "SELECT * FROM Lektion";
SqlDataAdapter daLektion = new SqlDataAdapter(selCommandLektion);
daLektion.Fill(dsLCard, "Lektion");

SqlCommand selCommandLektionLernkarten = conn.CreateCommand();
selCommandLektionLernkarten.CommandText = "SELECT * FROM
LektionLernkarten";
SqlDataAdapter daLektionLernkarten = new
SqlDataAdapter(selCommandLektionLernkarten);
daLektionLernkarten.Fill(dsLCard, "LektionLernkarten");


dsLCard.Relations.Add("relLektion_LektionLernkarten",
dsLCard.Tables["Lektion"].Columns["ID"],
dsLCard.Tables["LektionLernkarten"].Columns["IDLektion"]);

dgLektion.DataSource = dsLCard.Tables["Lektion"];
dgLektionLernkarten.DataSource =
dsLCard.Tables["Lektion"].DefaultView;
dgLektionLernkarten.DataMember = "relLektion_LektionLernkarten";


Thanks
Dirk
 
D

Dirk W

Cor,

now it works perfect! Thanks

Btw. VB-Tips is a great side!

Dirk

Dirk,

Have a look at the bottom of this sample.

http://www.vb-tips.com/default.aspx?ID=d667d78f-4b08-42ef-ba3b-8b8620d93761

(And be aware that you are using the defaultview, what I would not do)

(be aware that the difference in this sample between VB and C# is only the
declaration, the ending ; and tho use [] for a property instead of () )

I hope this helps,

Cor

Dirk W said:
I setup a simple database with a master and a detail table. Both are
connected via a datarelation. Data of each table is shown in a
datagridview (dgLektion and dgLektionlernkarten). When I start the app
for the first time, everything works fine: all master rows are shown
in dgLektion and the appropriate child rows of the first selected
master row are shown in dgLektionlernkarten.

But if I click e.g. on the 2nd row of the master table,
dgLektionlernkarten is not updated and therefore still shows the child
rows of the previous selected master row.

Even more strange for me is that if I click on the column header in
dgLektion and so change the order of the master rows, than
dgLektionlernkarten is updating correctly and showing the child rows
for the now topmost master row.

Can anyone give me a hint what I am doing wrong?

Code:

SqlCommand selCommandLektion = conn.CreateCommand();
selCommandLektion.CommandText = "SELECT * FROM Lektion";
SqlDataAdapter daLektion = new SqlDataAdapter(selCommandLektion);
daLektion.Fill(dsLCard, "Lektion");

SqlCommand selCommandLektionLernkarten = conn.CreateCommand();
selCommandLektionLernkarten.CommandText = "SELECT * FROM
LektionLernkarten";
SqlDataAdapter daLektionLernkarten = new
SqlDataAdapter(selCommandLektionLernkarten);
daLektionLernkarten.Fill(dsLCard, "LektionLernkarten");


dsLCard.Relations.Add("relLektion_LektionLernkarten",
dsLCard.Tables["Lektion"].Columns["ID"],
dsLCard.Tables["LektionLernkarten"].Columns["IDLektion"]);

dgLektion.DataSource = dsLCard.Tables["Lektion"];
dgLektionLernkarten.DataSource =
dsLCard.Tables["Lektion"].DefaultView;
dgLektionLernkarten.DataMember = "relLektion_LektionLernkarten";


Thanks
Dirk
 
Top