Calculating average value in datagrid

  • Thread starter Thread starter Marcus Phass
  • Start date Start date
M

Marcus Phass

Let's say that we have an application for tracking vehicle gas consumption.
So, we have the following fields:

VehicleID, gasFillDate, mileage, litres

So, when someone fills the gas tank (allways to the full), he/she should
note the actual mileage (in moment of filling), and other data is printed on
the receipt.

I would like to calculate the average consumption in the moment of each gas
filling. This is the formula:
litres / (currentMileage - previousMileage) * 100

I would like to display these results in the asp.net datagrid. How can I do
these calculations for each row in the datagrid?
 
If would probally be more efficient in the database to retrieve these value,
but you can create an item template and do a calculation as follows

(c#)
<asp:TemplateColumn headertext="CalculatedColumn">
<ItemTemplate>
<%# (Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"Litres") )/(Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"CurrentMilage") - Convert.ToDouble(DataBinder.Eval(Container.DataItem,
"PreviousMileage") ) ))) * 100 %>
</ItemTemplate>
</asp:TemplateColumn>
 
Thanks!
But the problem is not that simple, because PreviousMileage is a "mileage"
field from previous row. So, somehow I need to acces the values from both -
the current and previous row, then subtract them (and do other calc.), and
then show the result in the current row.
 
Back
Top