Search code examples
sqlvb.netgridviewboundfield

VB.Net Calculating values using GridView bound fields and outputting value to another field


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?


Solution

  • 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>