Show enum Display Name in grid

Consider this enum:


public enum MailingPreference

{
    Unknown = 0,

    [Display(Name = "No mailing")]
    NoMailing = 1,

    Email = 2,

    Post = 3,

    [Display(Name = "Post and email")]
    PostAndEmail = 4,
}


And this grid column, bound to a property containing the above enum:

<GridColumn Field="@nameof(Contact.MailingPreference)" HeaderText="Mailing Preference" />


How can I get the grid to show the display names specified on the enum declaration?

It seems this would be a useful default behaviour, if it is not already possible somehow.



3 Replies

BL Balamurugan Lakshmanan Syncfusion Team July 12, 2023 03:26 PM UTC

Hi Matt,


Greeting from Syncfusion Support.


From your query, we understood that you need to show the enum display name instead of enum string or value. We have achieved your requirement on the attached sample with the help of column Template and GetDisplayName() method . We have attached the sample code and sample file for your reference. Kindly refer the attachments to resolve your issues.


@page "/"

 

@using Syncfusion.Blazor.Grids

@using Syncfusion.Blazor.DropDowns

@using System;

@using System.ComponentModel.DataAnnotations;

@using System.Linq;

@using System.Reflection;

 

 

 

 

 

<SfGrid @ref="Grid" DataSource="@Orders" AllowFiltering="true">

    <GridColumns>

        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>

        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>

        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>

        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>

        <GridColumn Field=@nameof(Order.Type) HeaderText="Type" Type="ColumnType.String" Width="130">

            <Template>

             

 

                @{

                    var data = (context as Order);

                    MailingPreference promo = MailingPreference.NoMailing;

 

                    if(data.Type==MailingPreference.NoMailing)

                    {

                        var a = GetDisplayName(MailingPreference.NoMailing);

                        <span>@a</span>

                    }

                    else if (data.Type == MailingPreference.PostAndEmail)

                    {

                        var a = GetDisplayName(MailingPreference.PostAndEmail);

                        <span>@a</span>

                    }

                    else

                    {

                        <span>@data.Type</span>

                    }

                }

               

               

                

            </Template>

            <FilterTemplate>

                <SfDropDownList Placeholder="Type" ID="Type" Value="@((string)(context as PredicateModel).Value)" DataSource="@FilterDropData" TValue="string" TItem="Data">

                    <DropDownListEvents TItem="Data" ValueChange="Change" TValue="string"></DropDownListEvents>

                    <DropDownListFieldSettings Value="Type" Text="Type"></DropDownListFieldSettings>

                </SfDropDownList>

            </FilterTemplate>

        </GridColumn>

    </GridColumns>

</SfGrid>

 

 

@code {

    public SfGrid<Order> Grid;

    public List<Order> Orders { get; set; }

 

    protected override void OnInitialized()

    {

        Random rnd = new Random();

        var values = Enum.GetValues(typeof(MailingPreference));

        Orders = Enumerable.Range(1, 75).Select(x => new Order()

            {

                OrderID = 1000 + x,

                CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],

                Freight = 2.1 * x,

                OrderDate = (new DateTime[] { new DateTime(2010, 5, 1), new DateTime(2010, 5, 2), new DateTime(2010, 5, 3), })[new Random().Next(3)],

                Type = (MailingPreference)(values.GetValue(rnd.Next(values.Length)))

            }).ToList();

    }

 

    public class Order

    {

        public int? OrderID { get; set; }

        public string CustomerID { get; set; }

        public DateTime? OrderDate { get; set; }

        public double? Freight { get; set; }

        public MailingPreference Type { get; set; }

    }

 

    public enum MailingPreference

 

    {

        Unknown = 0,

 

        [Display(Name = "No mailing")]

        NoMailing = 1,

 

        Email = 2,

 

        Post = 3,

 

        [Display(Name = "Post and email")]

        PostAndEmail = 4,

    }

 

 

    //public enum FileType : short

    //{

    //    Base = 1,

    //    Replace = 2,

    //    Delta = 3

    //}

    public class Data

    {

        public string Type { get; set; }

        public string Value;

    }

 

    public static string GetDisplayName(Enum enumValue)

    {

        return enumValue.GetType()

                        .GetMember(enumValue.ToString())

                        .First()

                        .GetCustomAttribute<DisplayAttribute>()

                        .GetName();

    }

 

    //public static string GetDisplayName(Enum enumValue)

    //{

    //    return enumValue.GetType()?

    //                    .GetMember(enumValue.ToString())?

    //                    .First()?

    //                    .GetCustomAttribute<DisplayAttribute>()?

    //                    .Name;

    //}

    //public static class EnumExtensions

    //{

       

    //}

   

    List<Data> FilterDropData = new List<Data>

    {

         new Data() { Type= "Unknown" },

        new Data() { Type= "NoMailing" },

        new Data() { Type= "Email" },

        new Data() { Type= "Post" },

        new Data() { Type= "PostAndEmail" }

    };

    public async Task Change(ChangeEventArgs<string, Data> args)

    {

        if (args.Value == "Unknown")

        {

            await this.Grid.ClearFilteringAsync();

        }

        else

        {

            await this.Grid.FilterByColumnAsync("Type", "contains", args.Value);

        }

    }

}



Please get back to us if you have further queries.


Regards,

Bala.


Attachment: BlazorApp1_e8f7930f.zip


MA Matt July 12, 2023 03:31 PM UTC

Thank you very much for your response and the code sample.


Would this be a useful feature request, i.e. to automatically show enum display names in the grid?


Kind regards

Matt




BL Balamurugan Lakshmanan Syncfusion Team August 3, 2023 08:02 AM UTC

Hi Matt,


We have considered your requirement "Provide support to show Enum Display Name in grid". as an uncertain improvement. At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. It will be implemented in any of our upcoming releases.


https://www.syncfusion.com/feedback/45855/provide-support-to-show-enum-display-name-in-grid



You can communicate with us regarding the open features at any time using the above Feature Report page. Please let us know if you have any concerns.


Regards,

Bala.


Loader.
Up arrow icon