Please refer this
HTML
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="True" CellPadding="2"
Width="50%" ShowFooter="true">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="Black" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<PagerStyle BackColor="Black" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
I have created the dummy table here which is bound to the GridView.
Namespaces
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("ProductName", typeof(string)),
new DataColumn("Price",typeof(int)) });
dt.Rows.Add(1, "Bat", 750);
dt.Rows.Add(2, "Hat", 300);
dt.Rows.Add(3, "Ball", 50);
gvProducts.DataSource = dt;
gvProducts.DataBind();
int total = 0;
foreach (GridViewRow row in gvProducts.Rows)
{
int price = Convert.ToInt32(row.Cells[2].Text);
total += price;
}
if (gvProducts.Rows.Count > 0)
{
int cellCount = this.gvProducts.Rows[0].Cells.Count;
this.gvProducts.FooterRow.Cells[0].ColumnSpan = cellCount;
this.gvProducts.FooterRow.Cells[0].Text = string.Format("Total is {0}", total);
for (int i = 1; i < gvProducts.FooterRow.Cells.Count; i++)
{
this.gvProducts.FooterRow.Cells[i].Visible = false;
}
}
}
}
Screenshot
