Enum Display in GridComboBoxColumn

I Have an enum like bellow


And the converter


As you can see, what i want is the Description will show the enum type as Display with string get from EnumDisplayConverter, and the result should be look like this.


but when i use with Syncfusion SfDatagrid like bellow


Then the Description Column just show Display string when i enter to editmode , and if i breakout of editmode , i become enum string value , show show display string as i need, can you help me what i need to do, thanks.

this is what i get when use with SfDataGrid.



1 Reply

VS Vijayarasan Sivanandham Syncfusion Team December 22, 2021 11:14 AM UTC

Hi Nguyen,

Based on provided information the reported problem occurs due to mismatch in underline DataType of GridComboBoxColumn. Your under line DataType is Enum but Display Name is string type. In this case, the reported problem occurs in GridComboBoxColumn.

Your requirement can be achieved by using DisplayBinding property in GridColumn. Please refer the below code snippet,

XAML Code Snippet: 
<syncfusion:SfDataGrid  ItemsSource="{Binding Orders}" 
                                AutoGenerateColumnsForCustomType="True" 
                                AutoGenerateColumnsModeForCustomType="Child" 
                                ColumnSizer="SizeToHeader" 
                                AllowEditing="True" 
                                SelectionMode="Extended" 
                                AutoGeneratingColumn="OnAutoGeneratingColumn" 
                                AllowSorting="False" 
                                AllowFiltering="False" 
                                x:Name="sfDataGrid" >            
        </syncfusion:SfDataGrid> 

C# Code Snippet: 
private void OnAutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e) 
{ 
            if (e.Column.MappingName == "Description") 
            { 
                ((e.Column as GridComboBoxColumn).DisplayBinding as Binding).Converter = new EnumDisplayNameConverter(); 
            } 
} 

C# Code Snippet of Converter: 
public class EnumDisplayNameConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            var display = value.GetType()?.GetMember(value.ToString())?.First().GetCustomAttribute<DisplayAttribute>(); 
            return display.Name; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    } 

Regards, 
Vijayarasan S 


Loader.
Up arrow icon