Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Performing calculations on data in gridview

Status
Not open for further replies.

mrp9090

Programmer
May 22, 2006
71
GB
Is it possible to perform calculations on data in your gridview in your ASP, or do you need to do any calculations in the OnRowDataBound event? Here is what I am trying to do :

<asp:TemplateField HeaderText="Target (%)" SortExpression="TargetPercentage">
<ItemTemplate>
<asp:Label ID="lblTargetPercentage" Text='<%# Eval("TargetPercentage")*100 %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>
 
Yes you can do calculations in the GridView itself although I'd be more inclined to do them in the database for your above example.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
How do you do them then, given that my example above doesn't work?
 
What "doesn't work"? Do you get any errors?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I get the error :

Operator '*' cannot be applied to operands of type 'object' and 'int'
 
Try converting your Eval("TargetPercentage") to an Integer


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I thought you couldn't do a data type conversion in the declaration of the gridview? I would be interested to know what the syntax is for doing this.

I've managed to get it working using this approach :

<asp:TemplateField HeaderText="Target (%)" SortExpression="TargetPercentage">
<ItemTemplate>
<asp:Label ID="lblTargetPercentage" Text='<%# String.Format("{0:###0.00}", GetAsPercentage(Eval("TargetPercentage"))) %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>


protected double GetAsPercentage(object objTarget)
{
double dblTargetAsPercentage = Convert.ToDouble(objTarget) * 100;

return dblTargetAsPercentage;
}
 
The syntax would be eaxtly the same as your function call but with Cint. I still don't think it's a very "clean" method though and would do this calculation on the database and return it as a field.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
What ca8msm means is do this.

SQL statement
Code:
select targetpercentage * 100 as MyPercentage
from blahblah

GridView code
Code:
<asp:Label ID="lblTargetPercentage" Text='<%# Eval("MyPercentage")*100 %>' runat="server"></asp:Label>
 
You wouldn't need to multiply by 100 again in the EVAL. Just use the sql that tperri suplied and display that result.
 
oops forgot to remove the *100 in the GridView code.

My Mistake.

Code:
<asp:Label ID="lblTargetPercentage" Text='<%# Eval("MyPercentage") %>' runat="server"></asp:Label>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top