Hi Smile,
Please refer below sample.
HTML
<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false" ShowFooter="true"
OnDataBound="gvDetails_DataBound">
<Columns>
<asp:BoundField DataField="Comapany" HeaderText="Comapany" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Debit" HeaderText="Debit" />
<asp:BoundField DataField="Credit" HeaderText="Credit" />
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Comapany", typeof(string)),
new DataColumn("Date"),
new DataColumn("Debit",typeof(decimal)),
new DataColumn("Credit",typeof(decimal))
});
dt.Rows.Add("A", "01-12-2021", "0", "500");
dt.Rows.Add("B", "01-12-2021", "1500", "0");
dt.Rows.Add("C", "02-12-2021", "1000", "0");
gvDetails.DataSource = dt;
gvDetails.DataBind();
gvDetails.FooterRow.Cells[1].Text = "Grand Total:";
gvDetails.FooterRow.Cells[1].Font.Bold = true;
gvDetails.FooterRow.Cells[1].BackColor = System.Drawing.Color.LightSteelBlue;
gvDetails.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
decimal totalDis = dt.AsEnumerable().Sum(row => row.Field<decimal?>("Debit")).Value;
decimal totalAr = dt.AsEnumerable().Sum(row => row.Field<decimal?>("Credit")).Value;
gvDetails.FooterRow.Cells[2].Text = totalDis.ToString();
gvDetails.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Center;
gvDetails.FooterRow.Cells[2].Font.Bold = true;
gvDetails.FooterRow.Cells[2].BackColor = System.Drawing.Color.Yellow;
gvDetails.FooterRow.Cells[3].Text = totalAr.ToString();
gvDetails.FooterRow.Cells[3].HorizontalAlign = HorizontalAlign.Center;
gvDetails.FooterRow.Cells[3].Font.Bold = true;
gvDetails.FooterRow.Cells[3].BackColor = System.Drawing.Color.Yellow;
}
}
protected void gvDetails_DataBound(object sender, EventArgs e)
{
GridView gvDetails = (GridView)sender;
GridViewRow footer = gvDetails.FooterRow;
var numCells = footer.Cells.Count;
GridViewRow newRow = new GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState);
for (int i = 0; i <= numCells - 1; i++)
{
TableCell emptyCell = new TableCell();
emptyCell.ApplyStyle(gvDetails.Columns[i].ItemStyle);
newRow.Cells.Add(emptyCell);
}
DataTable dt = gvDetails.DataSource as DataTable;
decimal debit = dt.AsEnumerable().Sum(row => row.Field<decimal?>("Debit")).Value;
decimal credit = dt.AsEnumerable().Sum(row => row.Field<decimal?>("Credit")).Value;
newRow.Cells[2].Text = "Credit - Debit";
newRow.Cells[3].Text = (credit - debit).ToString();
((Table)gvDetails.Controls[0]).Rows.Add(newRow);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Comapany", GetType(String)),
New DataColumn("Date"),
New DataColumn("Debit", GetType(Decimal)),
New DataColumn("Credit", GetType(Decimal))
})
dt.Rows.Add("A", "01-12-2021", "0", "500")
dt.Rows.Add("B", "01-12-2021", "1500", "0")
dt.Rows.Add("C", "02-12-2021", "1000", "0")
gvDetails.DataSource = dt
gvDetails.DataBind()
gvDetails.FooterRow.Cells(1).Text = "Grand Total:"
gvDetails.FooterRow.Cells(1).Font.Bold = True
gvDetails.FooterRow.Cells(1).BackColor = System.Drawing.Color.LightSteelBlue
gvDetails.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
Dim totalDis As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal?)("Debit")).Value
Dim totalAr As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal?)("Credit")).Value
gvDetails.FooterRow.Cells(2).Text = totalDis.ToString()
gvDetails.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Center
gvDetails.FooterRow.Cells(2).Font.Bold = True
gvDetails.FooterRow.Cells(2).BackColor = System.Drawing.Color.Yellow
gvDetails.FooterRow.Cells(3).Text = totalAr.ToString()
gvDetails.FooterRow.Cells(3).HorizontalAlign = HorizontalAlign.Center
gvDetails.FooterRow.Cells(3).Font.Bold = True
gvDetails.FooterRow.Cells(3).BackColor = System.Drawing.Color.Yellow
End If
End Sub
Protected Sub gvDetails_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim gvDetails As GridView = CType(sender, GridView)
Dim footer As GridViewRow = gvDetails.FooterRow
Dim numCells = footer.Cells.Count
Dim newRow As GridViewRow = New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)
For i As Integer = 0 To numCells - 1
Dim emptyCell As TableCell = New TableCell()
emptyCell.ApplyStyle(gvDetails.Columns(i).ItemStyle)
newRow.Cells.Add(emptyCell)
Next
Dim dt As DataTable = TryCast(gvDetails.DataSource, DataTable)
Dim debit As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal?)("Debit")).Value
Dim credit As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal?)("Credit")).Value
newRow.Cells(2).Text = "Credit - Debit"
newRow.Cells(3).Text = (credit - debit).ToString()
CType(gvDetails.Controls(0), Table).Rows.Add(newRow)
End Sub
Screenshot