How to maintain the column order, width, sorting, grouping in WPF Grid?
How to maintain the column order, width, sorting, grouping in WPF Grid?
You can maintain the WPF DataGrid settings like column order, column width, sorting, grouping and etc. when navigating between page by using serialization and deserialization feature.
You can save the SfDataGrid settings in the file by using the SfDataGrid.Serialize method when you navigate to other pages. You can customize the serialization operation by setting SerializationOptions.
C#:
protected async override void OnNavigatedFrom(NavigationEventArgs e) { var folder = KnownFolders.DocumentsLibrary; try { var storageFile = await folder.CreateFileAsync("DataGrid.xml", CreationCollisionOption.ReplaceExisting); var options = new SerializationOptions(); options.SerializeColumns = true; this.SfdataGrid.Serialize(storageFile, options); } catch (Exception) { } base.OnNavigatedFrom(e); }
Then you can de-serialize the SfDataGrid setting from the saved file by using SfDataGrid.Deserialize method when you navigate to the same page again. You can customize the de-serialization operation by setting DeserializationOptions.
C#:
protected async override void OnNavigatedTo(NavigationEventArgs e) { //De-serializing the DataGrid when moving to current page var folder = KnownFolders.DocumentsLibrary; try { var storageFile = await folder.GetFileAsync("DataGrid.xml"); if (storageFile != null) { var options = new DeserializationOptions(); options.DeserializeColumns = true; this.SfdataGrid.Deserialize(storageFile, options); await storageFile.DeleteAsync(); } } catch (Exception) { } base.OnNavigatedTo(e); }
The above code snippet is applicable only for WinRT platform. For WPF, you can use the same method to maintain the SfDataGrid settings when closing and opening the application.
Samples
Conclusion
I hope you enjoyed learning about how to maintain the column order, width, sorting, grouping in WPF Grid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!