Hi, I'm using XamDataGrid recently. It works great except for a grouping issue. I group the data records by a field named "Section". I put the grouping setting in .xaml file like the following
<igDP: XamDataGrid>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:Field Name="Section" Visibility="Collapsed">
<igDP:Field.Settings>
<igDP:FieldSettings AllowHiding="Never"/>
</igDP:Field.Settings>
</igDP:Field>
<igDP:FieldLayout.SortedFields>
<igDP:FieldSortDescription IsGroupBy="True" Direction="Ascending" FieldName="Section">
</igDP:FieldLayout.SortedFields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP: XamDataGrid>
It works fine when I set the datasource of the datagrid for the first time. However, when I set the datasource to null and assign a new datasource (its structure is the same as the previous one) to the datagrid, the data records will not be grouped. I looped through the records property of the datagrid and found no groupbyrecord. But the info within XamDataGrid.FieldLayouts[0].SortedFields[0] remains the same. Can anyone tell me what is the probable cause.
Hello,
It could be that the XamDataGrid is creating a new field layout, which is not sorted. Does your new data source match the type of the old one? If this is true, then you can set the AutoGenerateFields property of the FieldlayoutSettings to false and it would work. If not, then you can handle the FieldLayoutInitialized event and add a new FieldSortDescription to the newly created layout.
Hi Alex,
Thanks for the prompt response. As for my data source, the field "section" is created manually , while the others are all obtained from database and their names can not be known in advance. Sometimes the new data source matches the type of the old one if user chooses the same parameter setting. But it can also vary due to different parameter setting. So I deem that I can not set the AutoGenerateFields property to false. Then I try to handle the FieldLayoutInitialized event as you suggest and move my group and sort setting as well as known field setting to the code file from the xaml file like the following (inside the FieldLayoutInitialized event handler)
dataGrid.FieldLayouts[0].Fields["Section"].Settings.GroupByComparer = new GroupByCustomComparer();
dataGrid.FieldLayouts[0].Fields["Section"].Visibility = Visibility.Collapsed;
dataGrid.FieldLayouts[0].Fields["Section"].Setting.AllowHiding = AllowFieldHiding.Never;
dataGrid.FieldLayouts[0].Fields["Statistics"].FixedLocation = FixedFieldLocation.FixedToNearEdge;
dataGrid.FieldLayouts[0].Fields["Statistics"].Setting.AllowHiding = AllowFieldHidng.Never;
FieldSortDescription description = new FieldSortDescription();
description.FieldName = "Section";
description.IsGroupBy = true;
description.Direction = ListSortDirection.Ascending;
dataGrid.FieldLayouts[0].SortedFields.Clear();
dataGrid.FieldLayouts[0].SortedFields.Add(description);
But the issue I mentioned still exists. And I found that it only occurs after I set the DataSource property of XamDataGrid to null. It works fine if I don't. Any suggestion?
You are still adding this in the first layout in the FieldLayouts collection. Please note that the FieldLayouts collection may have many FieldLayout objects. Also note that it is not necessary that the first layout will be applied. This is why, it is best to use the e.FieldLayout reference to the currently created one in that event.
Please try using e.FieldLayout instead of dataGrid.FieldLayouts[0].
Thanks for the advice. I followed your suggestion and also added a messagebox in the event handler. But it is weird that the messagebox only pops up in the first run. And my data in the subsequent runs stays ungrouped. In these runs my data sources all have the same structure with different amount of records. Is it the reason that the datagrid decides not to create new FieldLayouts objects? Even so the grouping setting of the initial FieldLayout object should apply to all of my testing data sources. Any idea? Thanks so much for your support.
Hi, Alex
My problem is solved by clearing the FieldLayouts of the XamDataGrid before repopulating it. Thanks a lot for all the wonderful help. Your suggestion corrects my understanding of how the FieldLayout object works. And without your guidance I can not find the solution in such a short time.