Skip to content

Populate DisplayAttribute for DataGrid content #2300

Closed

Description

@MykytaBortnyk

🙋 Feature Request

The Documentation says:

The DataGrid can generate column headers by using the System.ComponentModel.DataAnnotations.DisplayAttribute on properties shown in the grid.

Would be nice to have same behavior with column content. Yes, It may affect sorting.

🤔 Expected Behavior

Here's an example of some enum:

public enum Positions
{
    [Display(Name = "Employee")]
    Employee,
    [Display(Name = "HR Manager")]
    HrManager,
    [Display(Name = "Project Manager")]
    ProjectManager,
    [Display(Name = "Administrator")]
    Administrator
}

And example of an Employee model:

public class Employee
{
    [Display(Name = "Id")]
    public int Id { get; set; }
    [Required, Display(Name = "Full Name")]
    public Names FullName { get; set; }
    [Required, Display(Name = "Subdivision")]
    public Subdivision Subdivision { get; set; }
    [Required, Display(Name = "Position")]
    public Positions Position { get; set; }
    [Required, Display(Name = "Status")]
    public Status Status { get; set; }
    /// <summary>
    /// Navigation property
    /// </summary>
    [Required, ForeignKey("PeoplePartner"), Display(Name = "People Partner")]
    public Employee? Partner { get; set; }
    [Required, Display(Name = "Out-of-Office Balance")]
    public ushort? Balance { get; set; }
}

By adding DisplayAttribute to every enum value, I'm expecting same behavior as headers, but it doesn't.

😯 Current Behavior

Here's how it shows now:

Details

image

🔦 Context

I have a model, which properties are described by set of enum's, in current version I have to do additional steps to render my enums values as expected, instead of using same logic, as implemented for column headers.

Activity

  1. added
    triage
    on Jun 30, 2024
  2. vnbaaij commented on Jul 1, 2024

    @vnbaaij
    Collaborator

    I'm afraid this is currently not possible.

    Internally a Func<TGridItem, string?>? is defined to get the actual cell text (value) as a string. This is created from the expression put in in the PropertyColumn which is then converted to a string. I don't see a way to intercept/interrupt that step to do some additional work.

  3. franklupo commented on Jul 1, 2024

    @franklupo
    Contributor

    Temo che al momento questo non sia possibile.

    Internamente a Func<TGridItem, string?>?viene definito per ottenere il testo effettivo della cella (valore) come stringa. Questo viene creato dall'espressione inserita nella PropertyColumnche viene poi convertita in una stringa. Non vedo un modo per intercettare/interrompere quel passaggio per svolgere del lavoro aggiuntivo.

    in this line
    Check type is enum and get valur form attribute

  4. vnbaaij commented on Jul 1, 2024

    @vnbaaij
    Collaborator

    in this line Check type is enum and get value from attribute

    How?

  5. franklupo commented on Jul 1, 2024

    @franklupo
    Contributor

    in this line Check type is enum and get value from attribute

    How?

    var value = compiledPropertyExpression!(item);
    var valueTxt ="";
    
    if(typeof(TProp).IsEnum())
    {
    ....
    //get value from attribute like https://github.com/microsoft/fluentui-blazor/blob/3cf069c03d9046c61703432a03aae62c14daf42a/src/Core/Extensions/EnumExtensions.cs#L15
    //use reflection
    // value to enumvalue
    // enumvalue.GetDescription()
    ...
    }
    else 
    {
    valueTxt= value?.ToString();
    }
  6. vnbaaij commented on Jul 1, 2024

    @vnbaaij
    Collaborator

    But that piece of original code does not return the value. It returns the function to invoke to get the value

    Daniele Corsini (@franklupo) Are you on Discord? We can have more direct chat there...

  7. franklupo commented on Jul 1, 2024

    @franklupo
    Contributor

    But that piece of original code does not return the value. It returns the function to invoke to get the value

    Daniele Corsini (@franklupo) Are you on Discord? We can have more direct chat there...

    which link?

  8. vnbaaij commented on Jul 1, 2024

    @vnbaaij
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:in-progresstriage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions