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

StackOverflow https://stackoverflow.com/questions/15920231

سؤال

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?

هل كانت مفيدة؟

المحلول

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top