Hello,
I am using XamDatagrid where most of the columns are created dynamically. These columns are used to show the workload of a ressource per day. I also save the settings (visible columns, field width) by using this.mygrid.SaveCustomizations().
The user can define if he wants to see 2 or 10 weeks. In the case of a change of the number of weeks the XamDatagrid is created with a different set of columns than the saved customizations. In this case the customizations are not loaded at all.
I have seen this approach:
https://klausnji.wordpress.com/2014/04/08/infragistics-xamdatagrid-incorrectly-loading-and-saving-field-layout-when-model-changes/
Is there a solution from Infragistics out of the box as well?
Thanks
Niko
Hello Niko,
I have been investigating into this behavior you are reporting, and allow me to say that this behavior is actually expected - it is the way that the XamDataGrid carries out its customization loading. When loading customizations to the grid, the expectation is that at least the same Fields that you originally saved exist in the grid by Name.
What this means is that if you save a XamDataGrid with 6 Fields and then try to load into a grid with 2 of those 6 Fields, the customizations will fail to load, as we cannot make the assumption to know what to do with the other 4 Fields. However, if you were to save a grid with 2 Fields and then try to load into a grid with 6 Fields where the original 2 Fields exist by name, the customizations would be loaded successfully. This is expected, and by design of the XamDataGrid, and I am attaching a sample project where you can see this behavior for yourself by modifying the Field widths in the UI along with the int columnCount variable in the code-behind.
There does not currently exist a workaround to this behavior built into the product. As such, the best thing that I can recommend in this case is that in order to load your customizations, that you save the number of your Fields to an external xml or text file than the one that you are currently saving the customizations to. Alternatively, you can parse the existing one when you go to load it. You would then need to add Fields accordingly such that at least the number and names of the Fields that you saved exist in the grid prior to trying to load the customizations.
Please let me know if you have any other questions or concerns on this matter.
XDGCustomizationsCase.zip
Hello Andrew,
we solved this issue by checking the number of columns before XamDataGrid.LoadCustomizations() is applied. In this situation we were able to restore the sortorder and the visibility of the columns.
I am stuck currently to restore the position of the column.
Do you have a hint on that subject as well?
// arbeitseinteilungGrid is the string with the customization of the XamDatagrid
XDocument document = XDocument.Parse(arbeitseinteilungGrid);
// Restore Sortorder var sortedFields = fieldLayout.Element("sortedFields").Elements("sortedField"); var gridFieldLayout = this.Arbeitseinteilung.FieldLayouts[0]; foreach (var field in sortedFields) { gridFieldLayout.SortedFields.Add( new FieldSortDescription( field.Attribute("fieldName").Value, field.Attribute("direction").Value == "Ascending" ? ListSortDirection.Ascending : ListSortDirection.Descending, field.Attribute("isGroupBy").Value == "true" ) ); } // Check Visibility var visibleFields = fieldLayout.Element("fields").Elements("field"); foreach (var field in visibleFields) { string col = field.Attribute("name").Value.ToString();
// I check only the static, in XAML defined fields if (vmArbeitseinteilung.IgnoreColumns.Contains(col)) { var y = gridFieldLayout.Fields.FirstOrDefault(a => a.Name == col); if (y != null) { y.Visibility = field.Attribute("Visibility").Value == "Visible" ? Visibility.Visible : Visibility.Collapsed;
// HERE IS THE PROBLEM - which property stores the current positon of the field?
int pos; int.TryParse(field.Attribute("column").Value, out pos); if (pos > 0 && pos != y.ActualPosition.Column) { gridFieldLayout.Fields.Move(y.ActualPosition.Column, pos); } } }