Display Enum as Int

Hey,

I have an enum for Status like the following:

    public enum Status
    {
        Initial = 30,
        Added = 35,
        Edited = 40
    }

This is my model that the grid is bound to:

    public class Order
    {
          public int Id {get; set;}
          ...
          public Status StatusId {get; set;}
     }

However, my grid is displaying "Initial", "Added", or "Edited" for the StatusId property. I would like the numerical values displayed instead. The following is a snippet of my grid:

<syncfusion:GridTextColumn HeaderText="Status" MappingName="StatusId"   >
     <syncfusion:GridTextColumn.HeaderTemplate>
          <DataTemplate>
               <Label Text="Status" Style="{StaticResource headerGridStyle}" />
          </DataTemplate>
     </syncfusion:GridTextColumn.HeaderTemplate>
</syncfusion:GridTextColumn>

How would you achieve that? Thanks a lot in advance!

3 Replies

PK Pradeep Kumar Balakrishnan Syncfusion Team May 8, 2020 05:45 PM UTC

Hi Karim el Safadi,  
  
Thank you for contacting Syncfusion support.  
  
We have analyzed the requirement “How to set display Enum int value instead of Enum string value in DataGrid column” in Xamarin forms, we have prepared sample for your requirement. Please refer to the following code snippet for reference. In that we used the GridTemplate column and cell value is determined by converter. 
 
Code Snippet: 
<sfgrid:SfDataGrid x:Name="dataGrid" 
                AutoGenerateColumns="False" 
                ItemsSource="{Binding OrdersInfo}" > 
 
    <sfgrid:SfDataGrid.Columns> 
 
        <sfgrid:GridTextColumn MappingName="Id"/> 
 
        <sfgrid:GridTemplateColumn HeaderText="Status" MappingName="StatusId"> 
            <sfgrid:GridTemplateColumn.CellTemplate> 
                <DataTemplate> 
                    <Label Text="{Binding StatusId, Converter={StaticResource Key=CustomValue}}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/> 
                </DataTemplate> 
            </sfgrid:GridTemplateColumn.CellTemplate> 
        </sfgrid:GridTemplateColumn> 
 
        <sfgrid:GridTextColumn MappingName="StatusId"/> 
 
    </sfgrid:SfDataGrid.Columns> 
</sfgrid:SfDataGrid> 
 
 
[C#] 
 
public class CustomValue : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        if (value != null) 
        { 
            var numericValue = System.Convert.ChangeType(value, ((Status)Enum.Parse(typeof(Status), value.ToString())).GetTypeCode()); 
            return numericValue; 
        } 
 
        return string.Empty; 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 
 
Please let us know if you need further assistance.  
 
Regards, 
Pradeep Kumar B 



KE Karim el Safadi May 9, 2020 12:27 AM UTC

Thanks so much! Worked out perfectly!


PK Pradeep Kumar Balakrishnan Syncfusion Team May 11, 2020 05:43 AM UTC

HI Karim el Safadi 
 
Thank you for the update. We are glad to know that your requirement is fulfilled at your end.  
Please let us know, if you need any further assistance.  
 
Regards, 
Pradeep Kumar B 


Loader.
Up arrow icon