ASP GridView Calculated column

160 Views Asked by At

I Need to check your views on following.

I have a grid which has a calculated columns. The calculation is done using (Col A - col B) / SomeLabelAtTopofPage.Text

Question - Is it a good practice to have these calculation done in the grid declaration? (I personally do not like it, I like to keep my ASPX as clean as possible)

Example:

<asp:TemplateField HeaderText="Calculated Column" HeaderStyle-HorizontalAlign="Center"> <ItemTemplate> <span><%# (double.Parse(Eval("A").ToString()) - double.Parse(Eval("B").ToString())) / double.Parse(SomeLabelAtTopofPage.Text) %></span> </ItemTemplate> </asp:TemplateField>

Or is it better to do it in the DataBound Event of the grid?

var calcVal = ((Convert.ToDouble(e.Row.Cells[2].Text) - Convert.ToDouble(e.Row.Cells[3].Text)) / Convert.ToDouble(SomeLabelAtTopofPage.Text)) * 100;

e.Row.Cells[12].Text = string.Format("{0:0.000%}", calcVal);

I would rather do this at the source that is the stored procedure, but for some reasons I cannot modify the proc.

Can anyone guide me, on what would be a better way to do this and why?

Thank You

0

There are 0 best solutions below