Category / Section
How to use the same Converter for different columns regardless of the MappingName?
1 min read
In SfDataGrid, you can use the same converter for different columns even when you are not aware of the BusinessObject or MappingName used in the sample. This can be achieved by using the SfDataGrid.View.PropertyAccessProvider
Note: In the following sample, the format is applied for all the columns except string typed columns with the same Converter.
C#
public class CurrencyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var gridCell = value as GridCell; var record = gridCell.DataContext; var datagrid = gridCell.ColumnBase.Renderer.DataGrid; var cellvalue = datagrid.View.GetPropertyAccessProvider() .GetValue(record, gridCell.ColumnBase.GridColumn.MappingName); var collection = gridCell.DataContext.GetType().GetProperties().ToList(); foreach (var item in collection) { if (item.Name == gridCell.ColumnBase.GridColumn.MappingName) if (item.PropertyType.Name != "String") return "$" + " " + cellvalue.ToString(); } return cellvalue.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Sample Link: