Datagrid disappearing rows

Apr 10, 2009 at 8:28 AM

I'm struggeling with what I can only assume is a bug in the WPF DataGrid.

I have two datagrids inside a TabItem. One displays parent data (relations) and the second displays child date (contact persons) from the selected item of the first datagrid.
Whenever a new item is selected in the first DataGrid, the child data is loaded and bound to the ItemsSource of the second grid in code-behind. I use the CurrentCellChanged event for this.

All seems to work perfectly. Whenever I select a row in the first datagrid, the second datagrid correctly dispays the child data. But when I try to select a row in the second datagrid, suddenly all rows in that datagrid magically disappear. 

After some debugging, I found that after attempting to select a row in the second datagrid, the ItemsSource of that datagrid is actually empty, the DataView.Count property returns 0.

The strange thing is, if I put the focus on the second datagrid before assinging the new data to its ItemsSource, then it works correctly!
Unfortuately this is not an acceptable solution because users must be able to navigate the main datagrid using the keyboard. Putting the focus back on the main datagrid after binding doesn't work.

Hope someone can help me out.


Apr 10, 2009 at 2:30 PM
If you have a relationship set up between your 2 Tables, bind the ItemsSource of your Parent Grid to the Main Table Data and Bind your child DataGrid ItemsSource to the Relationship i.e. ItemsSource="FK_ContactPersons_Company". This should work. Good Luck!
Apr 11, 2009 at 3:05 PM
Edited Apr 11, 2009 at 3:06 PM
I'm not using a relationship. I do not fetch the child data from the database until asked for. However your solution sounds interesting and I'll give it a try. BTW, the same thing happens if I use a custom object collection as the child datasource instead of a datatable.

update: I found the issue also goes away if I do a <datatable>.AcceptChanges() after binding the child data source. I find this very strange because there is no bound relationship between the two datasources, and the main datasource cannot be modified. AcceptChanges() also steals away the focus from keyboard navigation though, so I now used a hack and placed this code in the MouseLeave event of the main datagrid. Not a very solid solution, but it works for me for now.