开发者

DataTable Relation problem with Datagrid (wpf)

开发者 https://www.devze.com 2023-03-08 14:45 出处:网络
I\'ve got a problem that is driving me insane. I created a DataSet with DataRelations and I want it to be displayed in a parent/child manner.

I've got a problem that is driving me insane.

I created a DataSet with DataRelations and I want it to be displayed in a parent/child manner.

To make it easy I only have 2 tables:

  1. test with id (int)(PK), col1 (string)
  2. test1 with id(int)(PK)(FK -> test>id), col1 (string)

I created a relation between test and test2 like this:

DataRelation rel = new DataRelation("testRel",  
new DataColumn[] { database1DataSet.test.IDColumn },  
new DataColumn[] { database1DataSet.test1.idColumn });

I set the DataContext of the Grid containing my two Datagrids to

<Grid DataContext="{Binding Source={StaticResource database1DataSet}, Path=test}"> 

then I created two DataGrids. The first one:

<DataGrid IsSynchronizedWithCurrentItem="True" Name="dataGrid1" ItemsSource="{Binding}"> 

The second one:

<DataGrid IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=testRel}" Name="dataGrid2">  

The Columns are of course bound to the proper ColumnIDs.

You can cycle to the parent grid and the child grids elements change, just like I wanted it to do.

When you add a new row to the child grid (via NewItemPlaceholder) it auto completes the relation value, in this case the id value in test1 is taken from the selected parent row (column id of test), just like you'd expect it to work.

Now if you add a new row to the MASTER grid by filling out the NewItemPlaceholder and submitting it by pressing "Enter" the item is created in the master grid and is commited to the database as well. But if you try to add child row for this, newly added, master row the weird behaviour takes place.

You click on the NewItemPlaceholder in the开发者_运维问答 child datagrid. The first thing to notice is, that there won't be the auto completion of the id-value like when doing this for already existing rows. The second one is, that the row dissapears immediately after pressing enter from the second grid. You can't even make it visible again by cycling through the parent grid. (The row in the dataset though). If you rerun the application the newly added rows appear and work like intended.

If you add a new row programmatically with

database1DataSet.test.AddtestRow(4, "asd");  

the werid behaviour won't take place.

The question is: why is adding a new row to the datatable via datagrid different from doing it programmatically and how can I fix it/build a nice workaround?

Thanks in advance

P.S. if you need the Code you can download it at https://rapidshare.com/files/2456035623/WpfApplication1.zip (VS2010 Solution)


I had the same problem myself until I added AutoIncrement, AutoIncrementSeed and AutoIncrementStep on the primary columns.

test.Columns["ID"].AutoIncrement = true;
test.Columns["ID"].AutoIncrementSeed = -1;
test.Columns["ID"].AutoIncrementStep = -1;

After adding the code above on both tables (test and test1) the child grid would update with the correct id from the master grid.


Ok a really crappy workaround is to subscribe to the rowchanged event of that specific datatable, then create a new row of that type and copy the values from the row passed to the event into it. (Of course only when the event was an add event). After that you have to remove the current row and insert the newly created one by calling AddRow() of the datatable.
This is, of course, really annoying

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号