DataGrid enum rendering -- ✔ resolved

Hello,

i've looked at filtering enum columns in documentation and it's awesome, that you can localize column values using resources and filtering dropdowns are created localized for you automagically.

Is it possible though to use the same technique when displaying datagrid rows itself? When my row items contain enum properties, texts in enum column in rows are raw enum values and not those localized ones (through Display attribute). Sure, you can write extension to get localized property value name from Display attribute and use it in every column through template. But that's relatively tedious and i'm wondering if there is even use case when you would rather display raw enum values instead of nice localized ones.

Maybe there is a way how to do it now without templating every column with enum?.

Indeed this is not supported at the moment however it will be in our next update Monday:

1 Like

Hi Vladimir,
I saw this example, which is clear and straightforward;
However, you were free to define your types (GenderType, StatusType,.. ) as enums;
But in my case, I have my field as "int" in the database, and when scaffolding, it also comes as "int" as expected, so I thought I could create a getter property that returns an enum for my filter to work:

// in the entity class:
[Required]
public int ReqStatus { get; set; }

// in my partial class for the same entity:
public enum ReqStatusEnum
{
	Pending,
 	Approved,
	Rejected,
	Canceled
}
public ReqStatusEnum ReqStatusAsEnum => (ReqStatusEnum)ReqStatus;

And used it with my datagrid:

<RadzenDataGridColumn TItem="Xxx" Property="ReqStatusAsEnum" Title="Status" />

Which displayed the values correctly, and also made the filter show the enum values as a list for selection.
But my problem is when I select any of the filter values (e.g. 'Approved'): EF will through an error that the property ReqStatusAsEnum could not be translated:

The LINQ expression 'DbSet<Xxx>() .Where(c => c.ApprovalStatusAsEnum == Approved)' could not be translated. Additional information: Translation of member 'ReqStatusAsEnum' on entity type 'Xxx' failed...

Where did I mess up :grimacing:?

EF cannot translate your enums to SQL - you need to use int as in database.

I understand, but then what is the best way to achieve what I am trying to do?
I want to allow my users to filter by selecting "readable" text values (not numbers) from the filter menu..

Hi, could you please show some example how to implement that properly?

When I create just Enum like this:

        public enum AnnualLeaveType
        {
            [Display(Description = "AnnualLeave", ResourceType = typeof(EnumsLibrary))]
            AnnualLeave,
            [Display(Description = "OnDemandLeave", ResourceType = typeof(EnumsLibrary))]
            OnDemandLeave,
            [Display(Description = "AdditionalAnnualLeave", ResourceType = typeof(EnumsLibrary))]
            AdditionalAnnualLeave,
            [Display(Description = "OccasionallLeave", ResourceType = typeof(EnumsLibrary))]
            OccasionallLeave,
            [Display(Description = "ChildcareLeave", ResourceType = typeof(EnumsLibrary))]
            ChildcareLeave,
            [Display(Description = "TrainingLeave", ResourceType = typeof(EnumsLibrary))]
            TrainingLeave,
            [Display(Description = "JobSearchLeave", ResourceType = typeof(EnumsLibrary))]
            JobSearchLeave,
            [Display(Description = "RehabilitationLeave", ResourceType = typeof(EnumsLibrary))]
            RehabilitationLeave,
            [Display(Description = "BonusAnnualLeave", ResourceType = typeof(EnumsLibrary))]
            BonusAnnualLeave,
            [Display(Description = "EquivalentAnnualLeave", ResourceType = typeof(EnumsLibrary))]
            EquivalentAnnualLeave,
            [Display(Description = "EquivalentAdditionalAnnualLeave", ResourceType = typeof(EnumsLibrary))]
            EquivalentAdditionalAnnualLeave
        }

Then i get an error:
System.InvalidOperationException: Cannot retrieve property 'Description' because localization failed. Type '[MyPath].EnumsLibrary' is not public or does not contain a public static string property with the name 'AnnualLeave'.
at System.ComponentModel.DataAnnotations.LocalizableString.

The bypass I found is adding static strings like that, but it doesn't look like proper solution of problem:

public static readonly ResourceManager _resourceManager = new ResourceManager("[MyPath].EnumsLibrary", typeof(EnumsLibrary).Assembly);

public static string AnnualLeave => _resourceManager.GetString("AnnualLeave") ?? AnnualLeave;

This works for me...

public enum Test
{
	[Display(ResourceType = typeof(Enums), Description = nameof(Enums.Test_Added))]
	ADDED,
	[Display(ResourceType = typeof(Enums), Description = nameof(Enums.Test_Failed))]
	FAILED
}

Enums is resource file and Test_Added and Test_Failed are keys in there.

@ns6000 I have tried this way:

        public enum AnnualLeaveType
        {
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.AnnualLeave))]
            AnnualLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.OnDemandLeave))]
            OnDemandLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.AdditionalAnnualLeave))]
            AdditionalAnnualLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.OccasionallLeave))]
            OccasionallLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.ChildcareLeave))]
            ChildcareLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.TrainingLeave))]
            TrainingLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.JobSearchLeave))]
            JobSearchLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.RehabilitationLeave))]
            RehabilitationLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.BonusAnnualLeave))]
            BonusAnnualLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.EquivalentAnnualLeave))]
            EquivalentAnnualLeave,
            [Display(ResourceType = typeof(EnumsLibrary), Description = nameof(EnumsLibrary.AnnualLeaveType.EquivalentAdditionalAnnualLeave))]
            EquivalentAdditionalAnnualLeave
        }

But still getting an error:
Error: System.InvalidOperationException: Cannot retrieve property 'Description' because localization failed. Type 'EnumsLibrary' is not public or does not contain a public static string property with the name 'AnnualLeave'.

@Techoneshot
Enums in my example was not the whole library (Class Library) but one resource file.

For example, class library JizdniRady.Resources from one of my projects holding all the localizable resources for the whole application:
image

Then when you open resource BootstrapDateTimePicker.resx (without language suffix --> primary resource), please note that Access Modifier set to Public, that seems to be your problem, because by default, it's set to Internal
image

Sub-resources like for example BootstrapDateTimePicker.en.resx, can be set to No Code Generation, as so:

Then, example would be:

public enum Test
{
	[Display(ResourceType = typeof(BootstrapDateTimePicker), Description = nameof(BootstrapDateTimePicker.Clear))]
	CLEAR,
	[Display(ResourceType = typeof(BootstrapDateTimePicker), Description = nameof(BootstrapDateTimePicker.Close))]
	CLOSE
}

@ns6000 now all is working, thank you so much :slight_smile: