How to Load Different Cell Types for Each Row in WPF DataGrid?
You can load different controls for each row based on the column value by using GridTemplateColumn.CellTemplateSelector in WPF DataGrid (SfDataGrid).
XAML
<Window.Resources> <local:ViewModel x:Key="viewmodel" /> <local:DataTemplateSelectorExt x:Key="templateselctor"/> </Window.Resources> <syncfusion:SfDataGrid x:Name="datagrid" AllowEditing="True" AutoGenerateColumns="False" ItemsSource="{Binding OrderList}" ShowRowHeader="True" > <syncfusion:SfDataGrid.Columns> <syncfusion:GridComboBoxColumn MappingName="ProductName" ItemsSource="{Binding ComboBoxItemsSource,Source={StaticResourceviewmodel}}" /> <syncfusion:GridTemplateColumn HeaderText="Order ID" syncfusion:FocusManagerHelper.WantsKeyInput="True" MappingName="OrderID" CellTemplateSelector="{StaticResource templateselctor}"/> <syncfusion:GridTemplateColumn HeaderText="Product ID" syncfusion:FocusManagerHelper.WantsKeyInput="True" MappingName="ProductID" CellTemplateSelector="{StaticResource templateselctor}"/> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid>
The following code explains how different templates are loaded for each row based on the first column value.
public class DataTemplateSelectorExt : DataTemplateSelector { DataTemplate TextBoxTemplate; DataTemplate PercentTemplate; DataTemplate DoubleTemplate; DataTemplate CurrencyTemplate; DataTemplate UpdownTemplate; DataTemplate TextBlockTemplate; public DataTemplateSelectorExt() { TextBlockTemplate = App.Current.Resources["TextBlockTemplate"] as DataTemplate; TextBoxTemplate = App.Current.Resources["TextBoxTemplate"] as DataTemplate; PercentTemplate = App.Current.Resources["PercentTemplate"] as DataTemplate; DoubleTemplate = App.Current.Resources["DoubleTemplate"] as DataTemplate; CurrencyTemplate = App.Current.Resources["CurrencyTemplate"] as DataTemplate; UpdownTemplate = App.Current.Resources["UpdownTemplate"] as DataTemplate; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item == null) return TextBlockTemplate; DataContextHelper dataContextHelper = item as DataContextHelper; OrderInfo orderInfo = dataContextHelper.Record as OrderInfo; if (orderInfo == null) return TextBlockTemplate; switch (orderInfo.ProductName) { case "TextColumn": return TextBoxTemplate; case "NumericColumn": return PercentTemplate; case "DoubleTextBoxColumn": return DoubleTemplate; case "CurrencyColumn": return CurrencyTemplate; case "GridUpDownColumn": return UpdownTemplate; default: return TextBlockTemplate; } } }
The RecordPropertyChanged event of data grid is used to update the corresponding values to GridTemplateColumns when the first column value is changed. The CollectionChanged event is used to update the values of GridTemplateColumns based on the first column value when a record is added at runtime.
public class SfDataGridBehavior :Behavior<SfDataGrid> { SfDataGrid datagrid = null; protected override void OnAttached() { datagrid = this.AssociatedObject as SfDataGrid; datagrid.CellRenderers.Remove("Template"); datagrid.CellRenderers.Add("Template", new GridCellDataTemplateRenderer()); datagrid.Loaded += datagrid_Loaded; } private void datagrid_Loaded(object sender, RoutedEventArgs e) { this.datagrid.View.CollectionChanged += View_CollectionChanged; this.datagrid.View.RecordPropertyChanged += View_RecordPropertyChanged; } private void View_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.OldItems == null) return; var rowData = (e.OldItems[0] as Syncfusion.Data.RecordEntry).Data; var rowGenerator = this.datagrid.GetRowGenerator(); var dataRowBase = rowGenerator.Items.FirstOrDefault(row => row.RowData == rowData); if (dataRowBase != null) { var propertyinfo = dataRowBase.GetType().GetProperty("VisibleColumns", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); var columns = propertyinfo.GetValue(dataRowBase) as List<DataColumnBase>; foreach (var dataColumn in columns.Where(column => column.Renderer != null && column.GridColumn != null)) { dataColumn.UpdateBinding(rowData, false); } } } private void View_RecordPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName != "ProductName") return; var rowGenerator = this.datagrid.GetRowGenerator(); var dataRowBase = rowGenerator.Items.FirstOrDefault(row => row.RowData == sender); if (dataRowBase != null) { var propertyinfo = dataRowBase.GetType().GetProperty("VisibleColumns", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); var columns = propertyinfo.GetValue(dataRowBase) as List<DataColumnBase>; foreach (var dataColumn in columns.Where(column => column.Renderer != null && column.GridColumn != null)) { dataColumn.UpdateBinding(sender, false); } } } }
Conclusion
I hope you enjoyed learning on how load different cell types for each row in WPF DataGrid.
You can refer to our WPF Grid 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 Grid 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!