Hi
I use the grid to display a hierarchical multi-level data. I bind the selected datarecord to another control. So that control 'displays' DataRow currently selected in xamDataGrid.
Is there a possibility to select a DataRecord in xamDataGrid having a DataRow only? Can SelectedRow be two-way bound to anything?
This is pretty critical for us right now
Thank you
Just for some background, there are 2 concepts regarding records in the xamDataGrid, active and selected. There can be any # of selected records so they are exposed via the grid's SelectedItems.Records collection. There can never be more than 1 active record so the grid exposes an ActiveRecord property.
Both of these return the Records. Record is the base class for DataRecord, GroupByRecord and ExpandableFieldRecord. In order to get to the underlying data item, in this case the DataRow or DataRowView, you need to cast the Record to a DataRecord which exposes a DataItem property, e.g.
DataRecord dr = grid.ActiveRecord as DataRecord;
if (dr != null )
{
DataRowView drv = dr.DataItem as DataRowView;
...
}
If you are setting up a binding in xaml it might look something like this:
I hope this helps.
Thanks for the answer, now it's more clear.
But I'm more interested in making xamDataGrid's ActiveRecord the target of a binding. So that changing some property (of DataRow type) would force the xamDataGrid to change its ActiveRecord. Is it possible to achive this through data-binding, without having to loop through all records?
Thanks for the answer again. The method you've mentioned works fine only if the grid was bound to a DataView and I search for a DataRowView. But if the grid is bound to a DataTable, searching for DataRow results in nothing found. I also tried to create a DataView using the source DataTable and then search by a DataRowView. But that didn't help either.
Besides, it seems it can't find anything on lower levels of hierarchy.
Please, could you help me with this? I don't understand what causes the problem. Why can't I find a DataRow which is definitely there?
Thanks in advance
I not sure what the problem is either. One thing to try is to use the DefaultView property that is exposed of DataTable.
I am curious as to to how you are binding the DataSource property of the xamDataGrid as well as the element that is the source of the ActiveRecord binding and what the DataRecord returns from its DataItem property e.g.
DataRecord dr = xamDataGrid1.ActiveRecord as DataRecord;
if ( dr != null )
object dataitem = dr.DataItem; // what type of object does this return
The ActiveRecord's DataItem is returned as DataRowView. No matter the grid is bound to a DataTable or a DataView.
But i still can't make it find anything on non-zero level. Using DefaultView of DataTable doesn't help either.
The grid is bound to a custom class, which exposes some properties. First off, the zero-level is set by this:
DataSource="{Binding Source={StaticResource FilledDatasetProvider}, Path=RootPlatforms}"
FilledDatasetProvider provides access to that class. The class has RootPlatforms property, which is DataView. That DataView is a slice of the whole DataTable it's from.
So this is the only case that search works. I get a DataRowView from that RootPlatforms DataView and pass it to the GetRecordFromDataItem(..) method.
Next, other field layouts are make like this: <infrNS:FieldLayout Key="PlatformsView">. The PlatformsView property is the DefaultView of the Platforms table. Plus, there are relations between dataTables (if that matters) so we can get hierarchy. So this method fills the grid, but i can't use the defaultView to find a DataRecord.
Also, i've tried to use something like this: <infrNS:FieldLayout Key="PlatformsTable">. This is binding right to the Platforms table.
Maybe there's some problem in recursion so the method has troubles to find anything on non-zero levels of hierarchy?
I think I see the problem. We are comparing the DataItem for equality in this routine instead of comparing the underlying DataRows. I will see that we correct it in our code.
In the meentime, here is a routine that should work:
throw new ArgumentNullException("dp");
throw new ArgumentNullException("dataItem");
// loop over the collection looking for a direct
// match with the dataItem
DataRecord descendant = GetDataRecordFromDataItem(rm.Sorted, dataItem, recursive);
dataItem = ((DataRowView)dataItem).Row;
if (itemToTest is DataRowView)
if (itemToTest == dataItem)
return null;
// loop over the records again to check for any descendant records
if (descendant != null)
Thanks for that! I'll try it.