This is a migrated thread and some comments may be shown as answers.

Applying DataAnnotations on Grid Columns

5 Answers 820 Views
Grid
This is a migrated thread and some comments may be shown as answers.
R.S.
Top achievements
Rank 1
R.S. asked on 11 Nov 2016, 12:49 PM

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

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 15 Nov 2016, 07:19 AM
Hello Ruben,

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
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
0
R.S.
Top achievements
Rank 1
answered on 15 Nov 2016, 11:50 AM

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?

0
Konstantin Dikov
Telerik team
answered on 17 Nov 2016, 08:33 AM
Hi,

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
Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
0
R.S.
Top achievements
Rank 1
answered on 21 Nov 2016, 06:36 AM

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.

0
Accepted
Konstantin Dikov
Telerik team
answered on 22 Nov 2016, 02:23 PM
Hi Ruben,

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:

Best Regards,
Konstantin Dikov
Telerik by Progress
Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
Tags
Grid
Asked by
R.S.
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
R.S.
Top achievements
Rank 1
Share this question
or