asp.net - How can I have a column in a GridView computed on the fly? -


i have following gridview:

<asp:gridview id="gv" autogeneratecolumns="false" runat="server">         <columns>             <asp:boundfield datafield="productname" headertext="item" />             <asp:boundfield datafield="unitcost" headertext="cost" dataformatstring="{0:c}" />             <asp:boundfield datafield="originalcount" itemstyle-horizontalalign="center" headertext="old count" />                       <asp:templatefield headertext="new count" itemstyle-horizontalalign="center" >                 <itemtemplate>                     <asp:textbox id="newcount"  width="20" runat="server" />                 </itemtemplate>             </asp:templatefield>         </columns>        </asp:gridview> 

and want add final 'total' column calculates

(originalcount - newcount) * unitcost 

and updates user enters number in newcount text box.

can .net, or need use java? if latter, how tell gridview cell update?

thanks in advance,

ben

updated new code (old answer below)

you need use javascript you're asking. there million different ways you'll need modify code below make work you.

create gridview follows:

<asp:gridview runat="server" id="gv1" autogeneratecolumns="false">         <columns>             <asp:boundfield datafield="productname" headertext="item" />             <asp:templatefield headertext="cost">                 <itemtemplate>                     <asp:label runat="server" id="unitcost" text='<%# string.format("{0:c}",eval("unitcost")) %>'/>                 </itemtemplate>             </asp:templatefield>              <asp:templatefield itemstyle-horizontalalign="center" headertext="old count">                 <itemtemplate>                     <asp:label runat="server" id="originalcount" text='<%# bind("originalcount") %>' />                 </itemtemplate>             </asp:templatefield>                      <asp:templatefield headertext="new count" itemstyle-horizontalalign="center" >                 <itemtemplate>                     <asp:textbox id="newcount"  width="20" runat="server" onblur="javascript:gettotal(this);"  />                 </itemtemplate>             </asp:templatefield>             <asp:templatefield headertext="total" itemstyle-horizontalalign="center" >                 <itemtemplate>                     <asp:label runat="server" id="lbltotal"></asp:label>                  </itemtemplate>             </asp:templatefield>         </columns>  </asp:gridview> 

next add following javascript head tag:

<script type="text/javascript">     function gettotal(obj) {         var rowindex = obj.id.substring(obj.id.lastindexof('_') + 1, obj.id.length);         var unitcost = document.getelementbyid('maincontent_gv1_unitcost_' + rowindex).innerhtml.replace("$", "") ;         var originalcount = document.getelementbyid('maincontent_gv1_originalcount_' + rowindex).innerhtml;         var newcount = document.getelementbyid('maincontent_gv1_newcount_' + rowindex).value;         document.getelementbyid('maincontent_gv1_lbltotal_' + rowindex).innerhtml = "$" + ((originalcount - newcount) * unitcost).tofixed(2);    } </script> 

there have it. when make change newcount, total automatically updated in far right column.


old answer

you can adding new templatefield , writing static method calculate total. off top of head similar what's below.

first, add following templatefield:

<asp:templatefield headertext="total" itemstyle-horizontalalign="center" >    <itemtemplate>       <%# gettotal((double)eval("originalcount"),(double)eval("newcount"),(double)eval("unitcost")) %>    </itemtemplate> </asp:templatefield> 

then, in code-behind, add following static method:

public static double gettotal(double originalcount, double newcount, double unitcost) {     return (originalcount - newcount) * unitcost; } 

the end result column desired calculated total.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -