Hi AbdulHaque,
I have created a sample which full fill your requirement you need to modify the code according to your need.
HTML
<div>
<asp:GridView ID="gvData" AutoGenerateColumns="false" OnDataBound="OnDataBound" OnRowCreated="OnRowCreated"
runat="server">
<Columns>
<asp:BoundField HeaderText="District" DataField="District" />
<asp:BoundField HeaderText="Year" DataField="Year" />
<asp:BoundField HeaderText="Total Works" DataField="Total Works" />
<asp:BoundField HeaderText="Completed" DataField="Completed" />
</Columns>
</asp:GridView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("District")
,new DataColumn("Year")
,new DataColumn("Total Works")
,new DataColumn("Completed")
});
dt.Rows.Add("Bellary", "2013-14", "72", "72");
dt.Rows.Add("Bidar", "2013-14", "57", "57");
dt.Rows.Add("Kalaburagi", "2013-14", "200", "200");
dt.Rows.Add("Kopal", "2013-14", "100", "100");
dt.Rows.Add("Raichur", "2013-14", "82", "77");
dt.Rows.Add("Yadgir", "2013-14", "81", "81");
dt.Rows.Add("Bellary", "2014-15", "225", "218");
dt.Rows.Add("Bidar", "2014-15", "162", "156");
dt.Rows.Add("Kalaburagi", "2014-15", "573", "485");
dt.Rows.Add("Kopal", "2014-15", "261", "249");
dt.Rows.Add("Raichur", "2014-15", "173", "107");
dt.Rows.Add("Yadgir", "2014-15", "248", "201");
gvData.DataSource = dt;
gvData.DataBind();
}
}
string currentYear = string.Empty;
int subTotal = 0;
int total = 0;
int subTotalRowIndex = 0;
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
subTotal = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
string year = dt.Rows[e.Row.RowIndex]["year"].ToString();
total += Convert.ToInt32(dt.Rows[e.Row.RowIndex]["Total Works"]);
if (year != currentYear)
{
if (e.Row.RowIndex > 0)
{
for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
{
subTotal += Convert.ToInt32(gvData.Rows[i].Cells[2].Text);
}
this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
subTotalRowIndex = e.Row.RowIndex;
}
currentYear = year;
}
}
}
private void AddTotalRow(string labelText, string value)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
row.Cells.AddRange(new TableCell[3] { new TableCell (), //Empty Cell
new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right},
new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } });
gvData.Controls[0].Controls.Add(row);
}
protected void OnDataBound(object sender, EventArgs e)
{
for (int i = subTotalRowIndex; i < gvData.Rows.Count; i++)
{
subTotal += Convert.ToInt32(gvData.Rows[i].Cells[2].Text);
}
this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
this.AddTotalRow("Total", total.ToString("N2"));
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("District"), New DataColumn("Year"), New DataColumn("Total Works"), New DataColumn("Completed")})
dt.Rows.Add("Bellary", "2013-14", "72", "72")
dt.Rows.Add("Bidar", "2013-14", "57", "57")
dt.Rows.Add("Kalaburagi", "2013-14", "200", "200")
dt.Rows.Add("Kopal", "2013-14", "100", "100")
dt.Rows.Add("Raichur", "2013-14", "82", "77")
dt.Rows.Add("Yadgir", "2013-14", "81", "81")
dt.Rows.Add("Bellary", "2014-15", "225", "218")
dt.Rows.Add("Bidar", "2014-15", "162", "156")
dt.Rows.Add("Kalaburagi", "2014-15", "573", "485")
dt.Rows.Add("Kopal", "2014-15", "261", "249")
dt.Rows.Add("Raichur", "2014-15", "173", "107")
dt.Rows.Add("Yadgir", "2014-15", "248", "201")
gvData.DataSource = dt
gvData.DataBind()
End If
End Sub
Private currentYear As String = String.Empty
Private subTotal As Integer = 0
Private total As Integer = 0
Private subTotalRowIndex As Integer = 0
Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs)
subTotal = 0
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dt As DataTable = TryCast(e.Row.DataItem, DataRowView).DataView.Table
Dim year As String = dt.Rows(e.Row.RowIndex)("year").ToString()
total += Convert.ToInt32(dt.Rows(e.Row.RowIndex)("Total Works"))
If year <> currentYear Then
If e.Row.RowIndex > 0 Then
For i As Integer = subTotalRowIndex To e.Row.RowIndex - 1
subTotal += Convert.ToInt32(gvData.Rows(i).Cells(2).Text)
Next
Me.AddTotalRow("Sub Total", subTotal.ToString("N2"))
subTotalRowIndex = e.Row.RowIndex
End If
currentYear = year
End If
End If
End Sub
Private Sub AddTotalRow(labelText As String, value As String)
Dim row As New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
row.BackColor = ColorTranslator.FromHtml("#F9F9F9")
'Empty Cell
row.Cells.AddRange(New TableCell(2) {New TableCell(), New TableCell() With { _
.Text = labelText, _
.HorizontalAlign = HorizontalAlign.Right _
}, New TableCell() With { _
.Text = value, _
.HorizontalAlign = HorizontalAlign.Right _
}})
gvData.Controls(0).Controls.Add(row)
End Sub
Protected Sub OnDataBound(sender As Object, e As EventArgs)
For i As Integer = subTotalRowIndex To gvData.Rows.Count - 1
subTotal += Convert.ToInt32(gvData.Rows(i).Cells(2).Text)
Next
Me.AddTotalRow("Sub Total", subTotal.ToString("N2"))
Me.AddTotalRow("Total", total.ToString("N2"))
End Sub
ScreenShot