Hi,
I have a gridview with columns as Item,price,quantity. I want to calculate for each row which should be like:
total=price*quantity and finally calculate the total for all the items.. How to do this? please guide
suchitra says:Its not like that.. But it is hard to explain it to some people
But use postbacks for each textbox you will be too expensive.
I have written an article on same with live demo. check out
http://www.aspsnippets.com/Articles/Calculate-Row-Total-and-Grand-Total-in-ASPNet-GridView-using-jQuery.aspx
Vinz code is good without using javascript at textchanged event.. But i want it at row command event
Since i have not used JQuery anytime
You have not used jQuery that's fine. But each time if you postback the server will be heavily affected.
BTW what you want on RowDataBound
<asp:GridView ID="GVItem" runat="server" AutoGenerateColumns="False" CssClass="GridItemStyle" Height="15px" ShowFooter="True" UseAccessibleHeader="False" Width="100%" onrowcreated="GVItem_RowCreated" onrowdatabound="GVItem_RowDataBound" onrowcommand="GVItem_RowCommand" BackColor="White"> <EmptyDataTemplate> <table cellspacing="0" rules="all" style="font-family:Tahoma; font-size:8.25pt; border-collapse:collapse; width:100%"> <tr style="background-color:White"> <th scope="col">Item Name</th> <th scope="col">Item Code</th> <th scope="col">Unit</th> <th scope="col">Quantity</th> <th scope="col">Item Price</th> <th scope="col">Total</th> <th scope="col">Pending Qty</th> </tr> <tr> <td> <asp:DropDownList ID="drpitemname" runat="server" CssClass="dropdownlistingrid"> </asp:DropDownList> </td> <td> <asp:DropDownList ID="drpitemcode" runat="server" CssClass="dropdownlistingrid"> </asp:DropDownList> </td> <td> <asp:DropDownList ID="drpunit" runat="server" CssClass="dropdownlistingrid"> </asp:DropDownList> </td> <td> <asp:TextBox ID="txtqty" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("QuotationItemQty") %>'></asp:TextBox> </td> <td> <asp:TextBox ID="txtitemprice" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("ItemPrice") %>'></asp:TextBox> </td> <td> <asp:TextBox ID="txtTotal" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("Total") %>'></asp:TextBox> </td> <td> <asp:TextBox ID="txtpendingqty" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("QuotationPendingQty") %>'></asp:TextBox> </td> <td> <asp:LinkButton ID="lnkadd" runat="server" CommandName="AddItem" ForeColor="Gray" Font-Bold="true" Font-Underline="false">add</asp:LinkButton></td> </tr> </table> </EmptyDataTemplate> <Columns> <asp:TemplateField HeaderText="Item Name"> <ItemTemplate> <%#Eval("ItemName") %> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="drpitemname" runat="server" CssClass="dropdownlistingrid"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Item Code"> <ItemTemplate> <%#Eval("ItemCode") %> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="drpitemcode" runat="server" CssClass="dropdownlistingrid"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Unit"> <ItemTemplate> <%#Eval("UnitName") %> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="drpunit" runat="server" CssClass="dropdownlistingrid"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <%#Eval("QuotationItemQty")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtqty" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("QuotationItemQty") %>'></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Item Price"> <ItemTemplate> <%#Eval("ItemPrice") %> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtitemprice" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("ItemPrice") %>'></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Total"> <ItemTemplate><%#Eval("Total") %></ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtTotal" runat="server" CssClass="GridTextBoxStyle" Text='<%#Eval("Total") %>'></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Pending Qty"> <ItemTemplate><%#Eval("QuotationPendingQty") %></ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtpendingqty" runat="server" CssClass="GridTextBoxStyle"></asp:TextBox> <asp:LinkButton ID="lnkadd" runat="server" CommandName="AddItem" ForeColor="Gray" Font-Bold="true" Font-Underline="false">add</asp:LinkButton> </FooterTemplate> </asp:TemplateField> </Columns> <HeaderStyle BackColor="White" Font-Bold="True" Height="20px" BorderStyle="Solid" BorderWidth="1px" /> </asp:GridView> I need to calculate the grandtotal for the total column
While most of the solutions on net are javascript still I found one
check this
http://www.aspdotnetcodes.com/ViewQuestion_398.aspx
© COPYRIGHT 2024 ASPSnippets.com ALL RIGHTS RESERVED.