Hi all,
I've been experimenting an error for hours :
I tried to add a sortedfield on a hierarchical xamdatagrid, and I got a nullreference exceptions until I expanded all rows.
Is there a way to bypass this error ?
HI dialogues,
If the rows have not been expanded, then the fieldlayouts for the children rows do not exist yet.
You could wait for the child row to expand, then apply the sort. Another option would be to expand the first row (provided it has chid rows) and force the layouts to be created.
I am attaching a sample that does this.
Sincerely, Matt Developer Support Engineer
Thank you for answering.
What I am doing is something like a filesystem tree, containing :
- Folders that can contain Folders and Files
- Files
So I've got 2 FieldLayouts and they are assigned to an Item by checking the item type on grid_AssigningFieldLayoutToItem method.
It displays fine.
If I try to sort the grid, I need to totally expand rows like this (for 2 levels :
Dim level = 2 While level > 0 For Each rec In grid.GetRecordsInView(True) rec.IsExpanded = True Next level -= 1 End WhileIf I try using such a recursive algorythm ViewableRecords like in your sample, the rowsget expanded, but I get the nullreference exception.Why ?I'd prefer using the ViewableRecords way I think is faster, but It doen't work for me.
In fact, event with GetRecordsInView, it does not event work (I believe it used to).
What build are you using?
Sincerely,. Matt Developer Support Engineer
I use IG WPF v11.2.
I put a sample test application where I Tried 2 different methods to expand the rows.
Both throw Nullrefexception.
In fact, it doe not work only if expand and sort are queued.
I joined a test project :
- The buttons "Expand and sort..." throw the exception
- If you try either "Expand Method 1" Or "expand Method 2" and then Push "Sort", It works !
I solved my issue like this. I don't know if there's a better way :
- I handle the layoutupdated event where I sort the rows
- Expand the grid (that will raise the event)
Private Sub ExpandAndSort() AddHandler Me._xamDataGrid.LayoutUpdated, AddressOf _xamDataGrid_LayoutUpdated Me.ExpandAllRows(Me._xamDataGrid.ViewableRecords, True) End Sub Private Sub _xamDataGrid_LayoutUpdated(sender As System.Object, e As System.EventArgs) Handles _xamDataGrid.LayoutUpdated SetSort() RemoveHandler Me._xamDataGrid.LayoutUpdated, AddressOf _xamDataGrid_LayoutUpdated End Sub Private Sub ExpandAllRows(vrc As ViewableRecordCollection, expand As Boolean) For Each rec As Record In vrc rec.IsExpanded = expand If rec.HasChildren Then ExpandAllRows(rec.ViewableChildRecords, expand) End If Next End Sub
Your solution is very good.
Thanks for the update.