Hi everyone
Do I able to group by any property from the model and don't use a column for it?
For example, I have View Model which has 3 properties: FirstName, LastName. And only FirstName and LastName will be visible on the UI, but I would like to group them by the Age.
Hello Gleb,
Thank you for contacting Infragistics. Are you auto-generating your fields in the for the field layout to be used by the XamDataGrid? The field must be present in the layout in order to groupby it. I am currently investigating whether or not you can groupby a hidden field, as you may need a custom groupby comparer to achieve this.
Let me know if you have any questions.
I am following up to confirm that while yes, there is nothing preventing you from grouping by a field that is not visible programmatically, however the "age" field will appear in the groupby box. I recommend hiding the groupby box for fields that are hidden by setting the grid's GroupByAreaLocation property to None to prevent the end user from ungrouping. Otherwise you can allow this.
To group programmatically please add the Age field to a new instance of FieldSortDescription and set the IsGroupBy property to true all after the grid is loaded (eg, Loaded event.
eg.
private void Xgrid_Loaded(object sender, RoutedEventArgs e) { FieldSortDescription ageGroup = new FieldSortDescription(); ageGroup.Direction = System.ComponentModel.ListSortDirection.Descending; ageGroup.FieldName = "Age"; ageGroup.IsGroupBy = true; //Code for xamDataGrid this.xgrid.FieldLayouts[0].SortedFields.Clear(); this.xgrid.FieldLayouts[0].SortedFields.Add(ageGroup); this.xgrid.FieldLayouts[0].Fields["Age"].Visibility = Visibility.Hidden; this.xgrid.GroupByAreaLocation = GroupByAreaLocation.None; }
private void Xgrid_Loaded(object sender, RoutedEventArgs e)
{
FieldSortDescription ageGroup = new FieldSortDescription();
ageGroup.Direction = System.ComponentModel.ListSortDirection.Descending;
ageGroup.FieldName = "Age";
ageGroup.IsGroupBy = true;
//Code for xamDataGrid
this.xgrid.FieldLayouts[0].SortedFields.Clear();
this.xgrid.FieldLayouts[0].SortedFields.Add(ageGroup);
this.xgrid.FieldLayouts[0].Fields["Age"].Visibility = Visibility.Hidden;
this.xgrid.GroupByAreaLocation = GroupByAreaLocation.None;
}
Sorry, I forgot to mention that I am using XamGrid.
Thank you for following up. The same principles apply to the XamGrid component. The group settings on the grid toggles the groupby functionality. Hidden columns can be programmatically added using the GroupbyColumns collection.
private void Xgrid_Loaded(object sender, RoutedEventArgs e) { xgrid.GroupBySettings.AllowGroupByArea = GroupByAreaLocation.Hidden; xgrid.ColumnLayouts[0].GroupBySettings.IsGroupable = true; // Age Column col2 = this.xgrid.Columns.DataColumns[2]; // Group Age by adding it to groupby collection this.xgrid.GroupBySettings.GroupByColumns.Add(col2); // setting its GroupBy property col2.IsGroupBy = true; // hiding age this.xgrid.Columns[2].Visibility = Visibility.Hidden; }
{ xgrid.GroupBySettings.AllowGroupByArea = GroupByAreaLocation.Hidden;
xgrid.ColumnLayouts[0].GroupBySettings.IsGroupable = true;
// Age
Column col2 = this.xgrid.Columns.DataColumns[2];
// Group Age by adding it to groupby collection
this.xgrid.GroupBySettings.GroupByColumns.Add(col2);
// setting its GroupBy property
col2.IsGroupBy = true;
// hiding age
this.xgrid.Columns[2].Visibility = Visibility.Hidden;