Is it possible on "GridColumn" to set the value of "HeaderText" from the Display attributte of the related class/field ?
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"
|
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; }
} |
|
<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;
} |
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; }
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; }
It works in the grid
GridColumn Field=@nameof(Account.Name) AutoFit="true"
but not in the EditForm
SfTextBox @bind-Value="@rowSelected.Name" Placeholder="@GetDisplayName("Name")" FloatLabelType="@FloatLabelType.Always"
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"
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;
}