Hi smile,
Refer below sample.
HTML
<asp:GridView ID="GridView7" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:BoundField DataField="A_Type" HeaderText="Category" />
<asp:BoundField DataField="Total" HeaderText="Total" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Data.DataTable pro = new System.Data.DataTable();
pro.Columns.Add("A_Type", typeof(string));
pro.Columns.Add("Total", typeof(decimal));
pro.Rows.Add("Revenue", 15000);
pro.Rows.Add("Expenses", 7000);
if (pro.Rows.Count > 0)
{
GridView7.Visible = true;
GridView7.DataSource = pro;
GridView7.DataBind();
GridView7.FooterRow.Cells[0].Text = "Revenu - Expenses = Proft:";
GridView7.FooterRow.Cells[0].Font.Bold = true;
GridView7.FooterRow.Cells[0].BackColor = System.Drawing.Color.YellowGreen;
GridView7.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
decimal revenue = pro.AsEnumerable().Where(x => x["A_Type"].ToString() == "Revenue").Sum(row => row.Field<decimal?>("Total")).Value;
decimal expenses = pro.AsEnumerable().Where(x => x["A_Type"].ToString() == "Expenses").Sum(row => row.Field<decimal?>("Total")).Value;
decimal totalDis = revenue - expenses;
GridView7.FooterRow.Cells[1].Text = totalDis.ToString();
GridView7.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
GridView7.FooterRow.Cells[1].Font.Bold = true;
GridView7.FooterRow.Cells[1].BackColor = System.Drawing.Color.Yellow;
}
else
{
GridView7.Visible = false;
Response.Write("No Record Found");
return;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim pro As System.Data.DataTable = New System.Data.DataTable()
pro.Columns.Add("A_Type", GetType(String))
pro.Columns.Add("Total", GetType(Decimal))
pro.Rows.Add("Revenue", 15000)
pro.Rows.Add("Expenses", 7000)
If pro.Rows.Count > 0 Then
GridView7.Visible = True
GridView7.DataSource = pro
GridView7.DataBind()
GridView7.FooterRow.Cells(0).Text = "Revenu - Expenses = Proft:"
GridView7.FooterRow.Cells(0).Font.Bold = True
GridView7.FooterRow.Cells(0).BackColor = System.Drawing.Color.YellowGreen
GridView7.FooterRow.Cells(0).HorizontalAlign = HorizontalAlign.Right
Dim revenue As Decimal = pro.AsEnumerable().Where(Function(x) x("A_Type").ToString() = "Revenue").Sum(Function(row) row.Field(Of Decimal?)("Total")).Value
Dim expenses As Decimal = pro.AsEnumerable().Where(Function(x) x("A_Type").ToString() = "Expenses").Sum(Function(row) row.Field(Of Decimal?)("Total")).Value
Dim totalDis As Decimal = revenue - expenses
GridView7.FooterRow.Cells(1).Text = totalDis.ToString()
GridView7.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Center
GridView7.FooterRow.Cells(1).Font.Bold = True
GridView7.FooterRow.Cells(1).BackColor = System.Drawing.Color.Yellow
Else
GridView7.Visible = False
Response.Write("No Record Found")
Return
End If
End If
End Sub
Screenshot