2

Within a GridView is it possible, using the output of two or more asp BoundFields, to calculate a value and then output this in it's own field?

eg calculating % from two bound fields:

Amount    Total    % (calculated field from Amount / Total * 100)
137       69       50.4 

or is it better to generate this calculation using SQL and output the result to it's own BoundField?

2
  • 1
    I'd probably do it in the SQL for sheer convenience - otherwise you'd be repeating the work by looping through the gridview and calculating for each row. Commented Apr 10, 2013 at 8:07
  • Good call Tim, thanks. Commented Apr 10, 2013 at 8:30

1 Answer 1

3

Use TemplateField instead of the BoundField

<asp:TemplateField HeaderText="Calculation">
    <ItemTemplate>
        <asp:TextBox ID="tb" runat="server" 
                     Text='<% ((Convert.ToDecimal(Eval("Amount"))/Convert.ToDecimal(Eval("Total")))*Convert.ToDecimal(100)).ToString() %>' >  
        </asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.