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

Casting in GridCalculatedColumn - Possible?

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marcus
Top achievements
Rank 1
Marcus asked on 13 Jun 2020, 12:01 AM

Hello All,

I'm trying to calculate a percentage in a GridCalculatedColumn, but both input types are integer, so the return value is incorrect (an int formatted as %, which results in 0%, 100% 200% etc.)

Is it possible to cast the value in the expression, or do I have to do it the hard way using itemdatabound or similar?

markup:
<telerik:GridCalculatedColumn DataFields="totalNo,UsedNo" HeaderText="Allocated" DataType="System.Decimal"
Expression="({0} / {1})" ColumnGroupName="Compute" DataFormatString="{0:P}" />

I tried "(Convert.ToDecimal({0}) .... but that gave me errors.

Thx in advance!
M.

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 17 Jun 2020, 11:09 AM

Hi Marcus,

The Calculated column will take into account the type of data of the fields. For instance, the fields bound to RadGrid will need to be of the correct type if you're looking to work with decimals.

Then you can use an expression to calculate the numbers you need.

<telerik:GridCalculatedColumn DataFields="totalNo,UsedNo" HeaderText="Allocated"  Expression="({0} / {1})" ColumnGroupName="Compute" DataFormatString="{0:P}" />
<telerik:GridCalculatedColumn DataFields="totalNo,UsedNo" HeaderText="No Data Formatting"  Expression="({0} / {1})" ColumnGroupName="Compute"  />

 

Result

 

You can also format that in the code behind and change the cell's values programmatically, see Accessing Cells.

 

Kind regards,
Attila Antal
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
commented on 24 Jan 2023, 06:35 PM

I think the correct Expression should be to get a percentage: 

Expression="({1} / {0})"

Tags
Grid
Asked by
Marcus
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or