From 20539d0c8379ab0df7d40618d5fb5cd37cc3187f Mon Sep 17 00:00:00 2001 From: KB Bot Date: Fri, 21 Mar 2025 14:38:04 +0000 Subject: [PATCH 1/2] Added new kb article grid-customize-enum-text --- knowledge-base/grid-customize-enum-text.md | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 knowledge-base/grid-customize-enum-text.md diff --git a/knowledge-base/grid-customize-enum-text.md b/knowledge-base/grid-customize-enum-text.md new file mode 100644 index 0000000000..7765b4b69b --- /dev/null +++ b/knowledge-base/grid-customize-enum-text.md @@ -0,0 +1,85 @@ +--- +title: How to Customize Enum Text Display +description: Learn how to customize the display of enum values by removing underscores and applying custom text. +type: how-to +page_title: How to Customize Enum Text Displays in Blazor Grid +slug: grid-kb-customize-enum-text +tags: grid, enum, display, customize +res_type: kb +ticketid: 1680753 +--- + +## Environment + + + + + + + + +
ProductGrid for Blazor
+ +## Description + +In scenarios where enums are used for filtering and enum values contain underscores, it might be necessary to display these values without underscores or with customized text. This knowledge base article answers the following questions: + +- How to remove underscores from enum values? +- How to display customized text for enum values? + +## Solution + +Use the `Display` attribute on your enum members to specify custom display names for enum values, which will be shown in the filter dropdown instead of the default enum names. + +`````RAZOR +@using System.ComponentModel.DataAnnotations; + + + + + + + + + +@code { + private List GridData { get; set; } + + protected override void OnInitialized() + { + GridData = new List(); + var rand = new Random(); + for (int i = 0; i < 100; i++) + { + GridData.Add(new Employee() + { + EmployeeId = i, + Name = "Employee " + i.ToString(), + AgeInYears = rand.Next(10, 80), + TestEnum = i % 2 == 0 ? TEST.Yes_Yes : TEST.No_No + }); + } + } + + public class Employee + { + public int? EmployeeId { get; set; } + public string Name { get; set; } + public int? AgeInYears { get; set; } + public TEST TestEnum { get; set; } + } + + public enum TEST + { + [Display(Name = "Yes Yes")] + Yes_Yes, + [Display(Name = "No No")] + No_No, + None + } +} +````` + From 0cd48025f1ad69f75a29c665dd3befd4ea974a39 Mon Sep 17 00:00:00 2001 From: Hristian Stefanov Date: Fri, 28 Mar 2025 17:04:40 +0200 Subject: [PATCH 2/2] kb(Grid): apply suggestions as per comments --- knowledge-base/grid-customize-enum-text.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/knowledge-base/grid-customize-enum-text.md b/knowledge-base/grid-customize-enum-text.md index 7765b4b69b..1947ccb4eb 100644 --- a/knowledge-base/grid-customize-enum-text.md +++ b/knowledge-base/grid-customize-enum-text.md @@ -41,7 +41,7 @@ Use the `Display` attribute on your enum members to specify custom display names - + @@ -59,7 +59,7 @@ Use the `Display` attribute on your enum members to specify custom display names EmployeeId = i, Name = "Employee " + i.ToString(), AgeInYears = rand.Next(10, 80), - TestEnum = i % 2 == 0 ? TEST.Yes_Yes : TEST.No_No + EmploymentStatus = i % 3 == 0 ? EmploymentStatus.Full_Time : (i % 3 == 1 ? EmploymentStatus.Part_Time : EmploymentStatus.Contractor) }); } } @@ -69,16 +69,16 @@ Use the `Display` attribute on your enum members to specify custom display names public int? EmployeeId { get; set; } public string Name { get; set; } public int? AgeInYears { get; set; } - public TEST TestEnum { get; set; } + public EmploymentStatus EmploymentStatus { get; set; } } - public enum TEST + public enum EmploymentStatus { - [Display(Name = "Yes Yes")] - Yes_Yes, - [Display(Name = "No No")] - No_No, - None + [Display(Name = "Full Time")] + Full_Time, + [Display(Name = "Part Time")] + Part_Time, + Contractor } } `````