Hi indradeo,
Use GridView OnRowDataBound event.
Refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="auto-style11"
Height="138px" Width="87%" ShowFooter="true" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="DEPARTMENT" HeaderText="DEPARTMENT" />
<asp:TemplateField HeaderText="Deviation">
<ItemTemplate>
<asp:LinkButton ID="Deviation" Text='<%# Eval("Deviation") %>' runat="server" CommandName="Deviation" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Near miss">
<ItemTemplate>
<asp:LinkButton ID="Near_miss" Text='<%# Eval("Near miss") %>' runat="server" CommandName="Near_miss" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Aid">
<ItemTemplate>
<asp:LinkButton ID="First_Aid" Text='<%# Eval("First Aid") %>' runat="server" CommandName="First_Aid" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fire">
<ItemTemplate>
<asp:LinkButton ID="Fire" Text='<%# Eval("Fire") %>' runat="server" CommandName="Fire" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Training">
<ItemTemplate>
<asp:LinkButton ID="Training" Text='<%# Eval("Training") %>' runat="server" CommandName="Training" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TBT/Pep Talk">
<ItemTemplate>
<asp:LinkButton ID="TBT_Pep_Talk" Text='<%# Eval("TBT_Pep_Talk") %>' runat="server" CommandName="TBT_Pep_Talk" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Non-Reportable">
<ItemTemplate>
<asp:LinkButton ID="NonReportable" Text='<%# Eval("Non-Reportable") %>' runat="server" CommandName="NonReportable" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reportable">
<ItemTemplate>
<asp:LinkButton ID="Reportable" Text='<%# Eval("Reportable") %>' runat="server" CommandName="Reportable" footertext="Grand Total:" footerstyle-font-bold="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fatal">
<ItemTemplate>
<asp:LinkButton ID="Fatal" Text='<%# Eval("Fatal") %>' runat="server" CommandName="Fatal" showfooter="true" autogeneratecolumns="false" />
</ItemTemplate>
<FooterTemplate>
<div style="padding: 0 0 5px 0">
<asp:Label ID="lblPageTotal" runat="server" />
</div>
<asp:Label ID="lblTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Count" HeaderStyle-CssClass="VerticalColumn">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("DEPARTMENT",typeof(string)),
new DataColumn("Deviation",typeof(int)),
new DataColumn("Near miss",typeof(int)),
new DataColumn("First Aid",typeof(int)),
new DataColumn("Fire",typeof(int)),
new DataColumn("Training",typeof(int)),
new DataColumn("TBT_Pep_Talk",typeof(int)),
new DataColumn("Non-Reportable",typeof(int)),
new DataColumn("Reportable",typeof(int)),
new DataColumn("Fatal",typeof(int))
});
dt.Rows.Add("HR", null, null, null, null, null, null, null, null, null);
dt.Rows.Add("IT", 1, 1, 1, 1, 1, 1, 1, 1, 1);
dt.Rows.Add("TS", 1, null, null, null, null, null, null, null, null);
GridView1.DataSource = dt;
GridView1.DataBind();
decimal deviation, near_miss, first_Aid, fire, non_Reportable, reportable, fatal, TBT_Pep_Talk, Training;
deviation = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Deviation")));
near_miss = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Near miss")));
first_Aid = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("First Aid")));
fire = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Fire")));
Training = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Training")));
TBT_Pep_Talk = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("TBT_Pep_Talk")));
non_Reportable = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Non-Reportable")));
reportable = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Reportable")));
fatal = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row.Field<int?>("Fatal")));
GridView1.FooterRow.Cells[0].Text = "Total Count";
GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;
GridView1.FooterRow.Cells[1].Text = deviation.ToString("N0");
GridView1.FooterRow.Cells[2].Text = near_miss.ToString("N0");
GridView1.FooterRow.Cells[3].Text = first_Aid.ToString("N0");
GridView1.FooterRow.Cells[4].Text = fire.ToString("N0");
GridView1.FooterRow.Cells[5].Text = Training.ToString("N0");
GridView1.FooterRow.Cells[6].Text = TBT_Pep_Talk.ToString("N0");
GridView1.FooterRow.Cells[7].Text = non_Reportable.ToString("N0");
GridView1.FooterRow.Cells[8].Text = reportable.ToString("N0");
GridView1.FooterRow.Cells[9].Text = fatal.ToString("N0");
}
}
decimal grandTotal = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string deviation = (e.Row.FindControl("Deviation") as LinkButton).Text.Trim();
string near_miss = (e.Row.FindControl("Near_miss") as LinkButton).Text.Trim();
string first_Aid = (e.Row.FindControl("First_Aid") as LinkButton).Text.Trim();
string fire = (e.Row.FindControl("Fire") as LinkButton).Text.Trim();
string training = (e.Row.FindControl("Training") as LinkButton).Text.Trim();
string tBT_Pep_Talk = (e.Row.FindControl("TBT_Pep_Talk") as LinkButton).Text.Trim();
string non_Reportable = (e.Row.FindControl("NonReportable") as LinkButton).Text.Trim();
string reportable = (e.Row.FindControl("Reportable") as LinkButton).Text.Trim();
string fatal = (e.Row.FindControl("Fatal") as LinkButton).Text.Trim();
decimal total = 0;
if (!string.IsNullOrEmpty(deviation)) { total += Convert.ToDecimal(deviation); }
if (!string.IsNullOrEmpty(near_miss)) { total += Convert.ToDecimal(near_miss); }
if (!string.IsNullOrEmpty(first_Aid)) { total += Convert.ToDecimal(first_Aid); }
if (!string.IsNullOrEmpty(fire)) { total += Convert.ToDecimal(fire); }
if (!string.IsNullOrEmpty(training)) { total += Convert.ToDecimal(training); }
if (!string.IsNullOrEmpty(tBT_Pep_Talk)) { total += Convert.ToDecimal(tBT_Pep_Talk); }
if (!string.IsNullOrEmpty(non_Reportable)) { total += Convert.ToDecimal(non_Reportable); }
if (!string.IsNullOrEmpty(reportable)) { total += Convert.ToDecimal(reportable); }
if (!string.IsNullOrEmpty(fatal)) { total += Convert.ToDecimal(fatal); }
(e.Row.FindControl("lblTotal") as Label).Text = total.ToString();
grandTotal += total;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
(e.Row.FindControl("lblFTotal") as Label).Text = grandTotal.ToString();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("DEPARTMENT", GetType(String)),
New DataColumn("Deviation", GetType(Integer)),
New DataColumn("Near miss", GetType(Integer)),
New DataColumn("First Aid", GetType(Integer)),
New DataColumn("Fire", GetType(Integer)),
New DataColumn("Training", GetType(Integer)),
New DataColumn("TBT_Pep_Talk", GetType(Integer)),
New DataColumn("Non-Reportable", GetType(Integer)),
New DataColumn("Reportable", GetType(Integer)),
New DataColumn("Fatal", GetType(Integer))
})
dt.Rows.Add("HR", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
dt.Rows.Add("IT", 1, 1, 1, 1, 1, 1, 1, 1, 1)
dt.Rows.Add("TS", 1, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
GridView1.DataSource = dt
GridView1.DataBind()
Dim deviation, near_miss, first_Aid, fire, non_Reportable, reportable, fatal, TBT_Pep_Talk, Training As Decimal
deviation = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Deviation")))
near_miss = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Near miss")))
first_Aid = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("First Aid")))
fire = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Fire")))
Training = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Training")))
TBT_Pep_Talk = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("TBT_Pep_Talk")))
non_Reportable = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Non-Reportable")))
reportable = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Reportable")))
fatal = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row.Field(Of Integer?)("Fatal")))
GridView1.FooterRow.Cells(0).Text = "Total Count"
GridView1.FooterRow.Cells(0).HorizontalAlign = HorizontalAlign.Center
GridView1.FooterRow.Cells(1).Text = deviation.ToString("N0")
GridView1.FooterRow.Cells(2).Text = near_miss.ToString("N0")
GridView1.FooterRow.Cells(3).Text = first_Aid.ToString("N0")
GridView1.FooterRow.Cells(4).Text = fire.ToString("N0")
GridView1.FooterRow.Cells(5).Text = Training.ToString("N0")
GridView1.FooterRow.Cells(6).Text = TBT_Pep_Talk.ToString("N0")
GridView1.FooterRow.Cells(7).Text = non_Reportable.ToString("N0")
GridView1.FooterRow.Cells(8).Text = reportable.ToString("N0")
GridView1.FooterRow.Cells(9).Text = fatal.ToString("N0")
End If
End Sub
Private grandTotal As Decimal = 0
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim deviation As String = TryCast(e.Row.FindControl("Deviation"), LinkButton).Text.Trim()
Dim near_miss As String = TryCast(e.Row.FindControl("Near_miss"), LinkButton).Text.Trim()
Dim first_Aid As String = TryCast(e.Row.FindControl("First_Aid"), LinkButton).Text.Trim()
Dim fire As String = TryCast(e.Row.FindControl("Fire"), LinkButton).Text.Trim()
Dim training As String = TryCast(e.Row.FindControl("Training"), LinkButton).Text.Trim()
Dim tBT_Pep_Talk As String = TryCast(e.Row.FindControl("TBT_Pep_Talk"), LinkButton).Text.Trim()
Dim non_Reportable As String = TryCast(e.Row.FindControl("NonReportable"), LinkButton).Text.Trim()
Dim reportable As String = TryCast(e.Row.FindControl("Reportable"), LinkButton).Text.Trim()
Dim fatal As String = TryCast(e.Row.FindControl("Fatal"), LinkButton).Text.Trim()
Dim total As Decimal = 0
If Not String.IsNullOrEmpty(deviation) Then
total += Convert.ToDecimal(deviation)
End If
If Not String.IsNullOrEmpty(near_miss) Then
total += Convert.ToDecimal(near_miss)
End If
If Not String.IsNullOrEmpty(first_Aid) Then
total += Convert.ToDecimal(first_Aid)
End If
If Not String.IsNullOrEmpty(fire) Then
total += Convert.ToDecimal(fire)
End If
If Not String.IsNullOrEmpty(training) Then
total += Convert.ToDecimal(training)
End If
If Not String.IsNullOrEmpty(tBT_Pep_Talk) Then
total += Convert.ToDecimal(tBT_Pep_Talk)
End If
If Not String.IsNullOrEmpty(non_Reportable) Then
total += Convert.ToDecimal(non_Reportable)
End If
If Not String.IsNullOrEmpty(reportable) Then
total += Convert.ToDecimal(reportable)
End If
If Not String.IsNullOrEmpty(fatal) Then
total += Convert.ToDecimal(fatal)
End If
TryCast(e.Row.FindControl("lblTotal"), Label).Text = total.ToString()
grandTotal += total
End If
If e.Row.RowType = DataControlRowType.Footer Then
TryCast(e.Row.FindControl("lblFTotal"), Label).Text = grandTotal.ToString()
End If
End Sub
Screenshot