I have an sfdatagrid which I have set to autogenerate columns and controlled the header and column order using DataAnnotations :-
[Display(Name = "Parameter Set", Order = 4)]
However, I want to make some of the columns ComboBoxColumns which means adding those columns explicitly. I've set the AutoGenerateColumnsMode property to Reset (I've also tried SmartReset) however the explicitly added columns all appear at the start of the grid rather than respecting the ordering defined in the DataAnnotations (I'm guessing the column has no way of referencing the underlying property when it's generated so can't access the data annotation either).
It's not a big deal and I can simply switch to defining all columns explicitly but it seems a shame to lose the neatness of the DataAnnotation approach. Is there a way of getting the explicitly added columns to respect the ordering in the data annotations?
Hi Declan Hillier,
We are able to understand your scenario. You can autogenerate and manually
define the columns in SfDataGrid. DataAnnotation attributes work only for
auto-generated columns. This is the behavior of SfDataGrid.
UG Link: https://help.syncfusion.com/wpf/datagrid/columns#data-annotations-with-autogeneratecolumns
If you are autogenerating the columns, then you can use the
AutoGenerationColumn event to add the GridComboboxColumn along with the
DataAnnotation attributes shown below,
|
this.sfDataGrid.AutoGeneratingColumn += OnAutoGeneratingColumn; private void OnAutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e) { //here check the Mapping which column type should change into GridComboBoxColumn if (e.Column.MappingName == "Country") { if (e.Column is GridTextColumn) { //here change the column type based on your scenario
//here add the ComboBoxColumn e.Column = new GridComboBoxColumn() { MappingName = "Country", HeaderText = "Country", ItemsSource = viewModel.CountryList,
}; } } } |
UG Link: https://help.syncfusion.com/wpf/datagrid/columns#changing-column-type
if you are adding the column at runtime in SfDataGrid. You can achieve this by
inserting the column into the specified index, as in the below mentioned code
snippet,
|
private void btnAddColumnClicked(object sender, RoutedEventArgs e) { //here insert the column into 3rd position in SfDataGrid sfDataGrid.Columns.Insert(2, new GridComboBoxColumn() { MappingName = "Country", HeaderText = "Country", ItemsSource = viewModel.CountryList, }); } |
Please find the sample in the attachment and let
us know if you have any concerns about this.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Attachment: Sample_344d9100.zip
Thanks Vijayarasan. I really like that approach of handling the AutoGeneratingColumn event. That means I can decide on the column type in the UI which feels like the right pla
Hi Declan Hillier,
If you are satisfied with our response, please mark it as an answer. Otherwise,
please let us know if you have any further queries on this. We are happy to
help you😊.
Regards,
Vijayarasan S