Hi, is it possible to have the Grid widget to use DataAnnotations when binding columns, in particular the DisplayFormat attribute and the the Display(ShortName) property?
I know that the GridBoundColumnBuilder has a Format method, but it would be better to avoid repeating information.
Also, when present, it would be preferable to use the ShortName property for the header in order to reduce column widths.
5 Answers, 1 is accepted
You can use DataAnnotations in the model and they will be handled by the Grid:
[DisplayName(
"Unit price"
)]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString=
"{0:n5}"
, ApplyFormatInEditMode=
false
)]
[Range(0,
int
.MaxValue)]
public
decimal
UnitPrice
{...}
However, have in mind that if you are using templates for displaying the values, the format will not be applied and you will have to handle it within the template.
Hope this helps.
Regards,
Konstantin Dikov
Telerik by Progress
Thank you. I actually saw that the DisplayFormat is correctly handled.
What about the ShortName property of the DisplayAttribute? Is there a way to tell the Grid to use ShortName when present and Name only as a fallback?
The ShortName property will not be used for the column headers and that value is retrieved either from the [DisplayName("name")] or [Display(Name ="name")]
You can however set the title for the columns directly in the Grid configuration:
columns.Bound(p => p.ProductName).Title("short title");
Regards,
Konstantin Dikov
Telerik by Progress
Your answer suggests that there is no way to make the Grid wrapper can't support a custom Attribute, right?
The reason I'm asking is that I also need to extract in a grid a list of records with amounts in multiple currencies.
Obviously, not al currencies have to be formatted the same way, as the number of decimal digits can differ from 0 to 3.
Is there a way to implement a column with dynamic formatting? I was thinking about a custom attribute, but now I don't really know how to solve it.
The formats that could be used in the columns are based on the current culture and mixing multiple currency formats is not supported scenario and the only thing that I can suggest is to use ClienTemplate and manually format the values.
Following are some links that might be helpful:
- http://docs.telerik.com/kendo-ui/framework/globalization/numberformatting
- http://docs.telerik.com/aspnet-mvc/helpers/grid/faq#how-to-use-javascript-functions-in-column-client-templates
Best Regards,
Konstantin Dikov
Telerik by Progress