hi..
How to create the sub total in data gridview in asp.net using c#
.aspx page
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
AutoGenerateColumns="false" onrowdatabound="GridView1_RowDataBound"
ShowFooter="True" onrowcreated="GridView1_RowCreated" CellPadding="5"
GridLines="Horizontal">
<Columns>
<asp:BoundField DataField="installmentNo" HeaderText="Instl No." SortExpression="installmentNo"/>
<asp:TemplateField HeaderText="Trans Date">
<ItemTemplate>
<%#Convert.ToDateTime(Eval("tDate")).Day.ToString() + "/" +Convert.ToDateTime(Eval("tDate")).Month.ToString() + "/" + Convert.ToDateTime(Eval("tDate")).Year.ToString()%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="introducerEmpCode" HeaderText="Introducer" SortExpression="introducerEmpCode"/>
<asp:BoundField DataField="username" HeaderText="Customer Code" SortExpression="username"/>
<asp:BoundField DataField="custName" HeaderText="Customer Name" SortExpression="custName"/>
<asp:BoundField DataField="installmentAmount" HeaderText="Instl Amount" SortExpression="installmentAmount"/>
<asp:TemplateField HeaderText="Charge (%) ">
<ItemTemplate>
<%#Eval("chargeInPercentage")%>
</ItemTemplate>
<FooterStyle Font-Bold="true"/>
<FooterTemplate>
Total :
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comm Credited">
<FooterStyle HorizontalAlign="Right"/>
<ItemStyle HorizontalAlign="Right"/>
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%# Eval("totalTransferAmount") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Font-Bold="true"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#cccccc" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="transactionMaster_S_AlltoEmpCode"
TypeName="NiwasTableAdapters.transactionMasterTableAdapter">
<SelectParameters>
<asp:CookieParameter CookieName="empCode" Name="toEmpCode" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
...............
aspx.cs page
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "totalTransferAmount"));
amount = amount + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((Label)e.Row.FindControl("lblTotal")).Text = amount.ToString();
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.