Show enum Display Name attribute value for enum in Grid

Is there any easy way to display an enum name attribute (text value) in a grid column? Ie I don't want to show the value (1,2,3, etc).  Something like the built in asp.net @Html.DisplayFor(model => model.DeviceType)

1 Reply

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team February 20, 2018 03:57 PM UTC

Hi Eric, 

Thanks for contacting Syncfusion Support. 

We have achieved your requirement using Newtonsoft serializer. Because, JavascriptSerializer serializes the enum to numeric values only. To serialize the enum values to their string representation we need to use Newtonsoft.json and we have achieved your requirement “Serializes the enum to their string representation in syuncfusion Grid” using custom serialize in datamanager.  
 
Please refer to the code example:-  

<ej-grid id="FlatGrid" allow-paging="true" > 
    <e-datamanager url="/Home/DataSource" adaptor="UrlAdaptor"></e-datamanager> 
    <e-columns> 
            .   .   . 
        <e-column field="val" header-text="val" width=@("80px")></e-column> 
    </e-columns> 
</ej-grid> 
 
 
Serverside:- 
 
     public IActionResult Index() 
        { 
            DataManagerConverter.Serializer = new DMSerial(); 
            .   .    . 
            return View(); 
        } 
 
 
      public class DMSerial : IDataSourceSerializer 
        { 
            public string Serialize(object obj) 
            { 
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 
                return str; 
            } 
        } 
 
Enum declaration:- 
 
       public enum weekdays { 
            [EnumMember(Value="Monday")] 
            Monday, 
            [EnumMember(Value = "Tuesday")] 
            Tuesday, 
            [EnumMember(Value = "Wednesday")] 
            Wednesday, 
            [EnumMember(Value = "Thursday")] 
            Thursday, 
            [EnumMember(Value = "Friday")] 
            Friday, 
            [EnumMember(Value = "Saturday")] 
            Saturday, 
            [EnumMember(Value = "Sunday")] 
            Sunday, 
        } 
 
     public void BindDataSource() 
        { 
            int code = 10000; 
            for (int i = 1; i < 10; i++) 
            { 
                order.Add(new Orders(code + 1, "ALFKI", i + 0, 2.3 * i, new DateTime(1991, 05, 15), "Berlin", weekdays.Monday)); 
                order.Add(new Orders(code + 2, "ANATR", i + 2, 3.3 * i, new DateTime(1990, 04, 04), "Madrid",weekdays.Thursday)); 
                 
                code += 5; 
            } 
        } 
 
           public DateTime OrderDate { get; set; } 
            public string ShipCity { get; set; } 
            [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] 
            public weekdays val { get; set; } 
        } 

Please refer to the sample Link:- 

Please refer to the KB link:- 

Please get back to us if you need any further assistance. 

Regards, 
Farveen sulthana T 


Loader.
Up arrow icon