Is it possible on "GridColumn" to set the value of "HeaderText" from the Display attributte of the related class/field ?

Is it possible on "GridColumn" to set the value of "HeaderText" from the Display attributte of the related class/field ?

GridColumn Field=@nameof(IdentityUserExtd.Name) HeaderText= ?????? 

public class Account {

[Display(Name = "Name on Account")]

public string Name { get; set; }

}

Also, the same for the EditForm and the PlaceHolder ?

EditForm

SfTextBox  @bind-Value="@model.Name" Placeholder="????" FloatLabelType="@FloatLabelType.Always"





6 Replies 1 reply marked as answer

MS Monisha Saravanan Syncfusion Team January 11, 2022 02:09 PM UTC


Hi Ben, 

Greetings from Syncfusion support. 

Query 1: Is it possible on "GridColumn" to set the value of "HeaderText" from the Display attributte of the related class/field ? 
 
Yes, we have provided Data Annotation support for Grid component. We can use DisplayName attribute of related property in class to set the required header text for the Grid column .Please refer the below code snippet and UG for additional information 

public class Order 
    { 
        [Key] 
        [Required(ErrorMessage ="Order ID  should not be empty")] 
        [Display(Name = "Order ID")] 
        public int? OrderID { get; set; } 
        [Display(Name = "Customer Name")] 
        [Required(ErrorMessage = "Field should not be empty")] 
        public string CustomerID { get; set; } 
        
    } 



Query 2: Also, the same for the EditForm and the PlaceHolder ? 
 
We have achieved your requirement by calling a public method in code section to find the Display Name Attribute of that particular property and returned to Placeholder property. Refer the below code example. 

<GridColumn Field="BlockStart10DigitInclusive" 
            TextAlign="TextAlign.Left" 
            EditType="EditType.NumericEdit"> 
        <EditTemplate> 
            <SfNumericTextBox ID="BlockStart10DigitInclusive" Placeholder="@GetDisplayName("BlockStart10DigitInclusive")" FloatLabelType="FloatLabelType.Always" TValue="long?" 
                                  @bind-Value="@((context as AABModel).BlockStart10DigitInclusive)" 
                                  ShowSpinButton="false"> 
                </SfNumericTextBox> 
        </EditTemplate> 
    </GridColumn> 
 
                         public string GetDisplayName(string col) 
    { 
    var name = ""; 
    MemberInfo property = typeof(AABModel).GetProperty(col); 
    var dd = property.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute; 
    if (dd != null) 
    { 
        name = dd.Name; 
    } 
 
    return name; 
    } 

Kindly get back to us if you have further queries. 

Regards, 
Monisha S 


Marked as answer

BJ Ben Junior January 11, 2022 09:49 PM UTC

Hi  Monisha S,

Thanks for your answer.  All worked as expected.

The only issue is that in the Account class I added a resource file for multiple languages and it works perfectly for the GridColumns but not for the EditForm: the GetDisplayName(string col) returns the Name as "AccountName" and not from the resource file, but I guess this is a problem with C# / Blazor. For now I am using 

SfTextBox  @bind-Value="@model.Name" Placeholder="@Resources.AccountName" and it works although it could be a lot of extra code. 

 

      //[Display(Name = "Name on Account")]

        [Display(ResourceType = typeof(@Resources), Name = "AccountName")]

        public string Name { get; set; }



 



MS Monisha Saravanan Syncfusion Team January 12, 2022 12:15 PM UTC

Hi Ben, 

Thanks for the update. 

We are glad to hear that you have found an alternative solution to achieve your requirement. And, we are not clear about the exact scenario of the problem you are facing. You can use the alternative solution or if you need any further assistance on the suggested solution from our previous update, then please share the below details for better assistance. 

  • Share a simple issue reproducible sample.
  • Share us video demonstration of the problem.
  • You have mentioned that you have used resources file. Kindly share the details about the resources file.
 
The above requested details will be helpful for us to validate your query and provide you with a better solution as early as possible. 

Regards, 
Monisha S 



BJ Ben Junior replied to Monisha Saravanan January 12, 2022 03:03 PM UTC

Hi  Monisha S ,

I apologize for the confusion: I did not find an alternative solution. I used the one you sent me and it worked perfectly. The problem is that after I implemented multi culture in the class, it didn't return the name translated to the related culture (In EditForm only, in the GridColumns it still worked perfectly). The resource file is any standard "Resources.resx". Below is a sample code.

public class Account  {

[Display(ResourceType = typeof(@Resources), Name = "AccountName")]

public string Name { get; set; }


Syncfusion_1.JPG


It works in the grid

GridColumn Field=@nameof(Account.Name) AutoFit="true" 

Syncfusion_2.JPG

but not in the EditForm

SfTextBox @bind-Value="@rowSelected.Name" Placeholder="@GetDisplayName("Name")" FloatLabelType="@FloatLabelType.Always"

Syncfusion_3.JPG


But if I revert the Display property in the class back to without multi culture, then it works ;

public class Account  {

[Display( Name = "Name on Account")]

public string Name { get; set; }


SfTextBox @bind-Value="@rowSelected.Name" Placeholder="@GetDisplayName("Name")" FloatLabelType="@FloatLabelType.Always"

Syncfusion_4.JPG




BJ Ben Junior January 13, 2022 03:13 AM UTC

Ok. I also asked on stackoverflow and got the answer from there. All that is needed to do is to add a call to the resource in your function: 


  public string GetDisplayName(string col)

    {

        var name = "";

        MemberInfo property = typeof(Account).GetProperty(col);

        var dd = property.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;

        if (dd != null)

        {

            name = Resources.ResourceManager.GetString(dd.Name);

        }

        return name;

    }



MS Monisha Saravanan Syncfusion Team January 13, 2022 11:32 AM UTC

Hi Ben, 

Thanks for your clarification. 

We are happy to hear that you have resolved your query on your own for your scenario using our suggestion.   

Please get back to us if you need further assistance. 

Regards, 
Monisha 


Loader.
Up arrow icon