Skip to content

Use property attributes to define text in the header of QuickGrid. #55616

@FelipeCostaGualberto

Description

@FelipeCostaGualberto

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Currently, QuickGrid's PropertyColumn gets the name of the property and use it as a title in the header of the table, as shown here.

It would be nice to get the text of the header based on the property's metadata.

Describe the solution you'd like

I suggest to get the text of the title following these rules, in this order of priority or another you judge better:

  1. [DisplayName] of property.
  2. [Display] of property.
  3. [DisplayName] of base property.
  4. [Display] of base property.
  5. Name of the property.

So, the code would be something like:

            var propertyExpression = (MemberExpression)Property.Body;
            var propertyName = propertyExpression.Member.Name;
            var propertyType = propertyExpression.Expression.Type;
            var propertyInfo = propertyType.GetProperty(propertyName);

            Title =
                propertyInfo.GetCustomAttributes<DisplayNameAttribute>(false).FirstOrDefault()?.DisplayName ??
                propertyInfo.GetCustomAttributes<DisplayAttribute>(false).FirstOrDefault()?.Name ??
                propertyInfo.GetCustomAttributes<DisplayNameAttribute>(true).FirstOrDefault()?.DisplayName ??
                propertyInfo.GetCustomAttributes<DisplayAttribute>(true).FirstOrDefault()?.Name ??
                propertyName;

In my tests, this works fine with localization if you declare a property like this:

    [Display(Name = nameof(AppResources.Name), ResourceType = typeof(AppResources))]
    public string CustomerName { get; set; }

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-quickgrid

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions