Hi akhter,
Check this sample. now take its reference and correct your code.
HTML
<asp:GridView ID="gvStock" runat="server" OnDataBound="gvStock_DataBound" OnRowCreated="OnRowCreated">
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Category"),
new DataColumn("Name"),
new DataColumn("Product"),
new DataColumn("Weight") ,
new DataColumn("Dispatch_Qty"),
new DataColumn("Dispatch_Weight"),
new DataColumn("Balance"),
new DataColumn("W_Balance"),
});
dt.Rows.Add("India(Uncut)", "High Nech LS", "7", "7310", "1", "1047", "6", "6263");
dt.Rows.Add("India(Uncut)", "Printed Fleece Bootom", "27", "18062", "18", "11610", "9", "4652");
dt.Rows.Add("India(Uncut)", "Uncut Cotton Jeans 100%", "1", "119", "1", "1119", "0", "0");
dt.Rows.Add("India(Uncut)", "Uncut Jeans", "240", "295265", "180", " 221058", "60", "74204");
dt.Rows.Add("India(Uncut)", "Uncut Sweater", "591", "582445", "467", "462902", "124", "119543");
dt.Rows.Add("India(Uncut)", "Uncut White Ploy", "30", "31960", "27", "28684", "3", "3276");
dt.Rows.Add("India(Uncut)", "Uncut Wool Body", "63", "57419", "63", " 57419", "0", "0");
dt.Rows.Add("India(Uncut)", "Uncut Wool Pants", "40", "43372", "40", " 43372", "0", "0");
dt.Rows.Add("India(Uncut)", "Uncut Wool Sweaters 50-100%", "2", "200", "2", "200", "0", "0");
dt.Rows.Add("India(Uncut)", "Uncut Wool Sweaters 60-100%", "26", "18071", "26", "18071", "0", "0");
dt.Rows.Add("India(Uncut)", "White Socks", "37", "31647", "36", " 30778", "1", "869");
dt.Rows.Add("India(Cut)", "High Nech LS", "327", "7310", "1", "1047", "6", "6263");
dt.Rows.Add("India(Cut)", "Printed Fleece Bootom", "146", "18062", "18", "11610", "9", "4652");
dt.Rows.Add("India(Cut)", "Cut Cotton Jeans 100%", "615", "119", "1", "1119", "0", "0");
dt.Rows.Add("India(Cut)", "Cut Jeans", "34", "295265", "180", " 221058", "60", "74204");
dt.Rows.Add("India(Cut)", "Cut Sweater", "106", "582445", "467", "462902", "124", "119543");
dt.Rows.Add("India(Cut)", "Cut White Ploy", "37", "31960", "27", "28684", "3", "3276");
dt.Rows.Add("India(Cut)", "Cut Wool Body", "106", "100218", "97", "91539", "9", "8679");
dt.Rows.Add("India(Cut)", "Cut Wool Pants", "37", "41179", "35", "39045", "0", "0");
dt.Rows.Add("India(Cut)", "Cut Wool Sweaters 50-100%", "1", "500", "1", "500", "0", "0");
dt.Rows.Add("India(Cut)", "Cut Wool Sweaters 60-100%", "57", "40870", "57", "40870", "0", "0");
this.gvStock.DataSource = dt;
this.gvStock.DataBind();
}
}
string currentCategory = "";
decimal wbSubTotal = 0;
decimal bSubTotal = 0;
decimal WBalanceTotal = 0;
decimal balanceTotal = 0;
int rowIndex = 0;
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
wbSubTotal = 0;
bSubTotal = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
string category = dt.Rows[e.Row.RowIndex]["Category"].ToString();
balanceTotal += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["Balance"]);
WBalanceTotal += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["W_Balance"]);
if (category != currentCategory)
{
if (e.Row.RowIndex > 0)
{
for (int i = rowIndex; i < e.Row.RowIndex; i++)
{
wbSubTotal += Convert.ToDecimal(gvStock.Rows[i].Cells[e.Row.Cells.Count - 1].Text);
bSubTotal += Convert.ToDecimal(gvStock.Rows[i].Cells[e.Row.Cells.Count - 2].Text);
}
this.AddTotalRow("Sub Total", bSubTotal.ToString("N2"), wbSubTotal.ToString("N2"));
rowIndex = e.Row.RowIndex;
}
currentCategory = category;
}
}
}
protected void gvStock_DataBound(object sender, EventArgs e)
{
for (int i = rowIndex; i < gvStock.Rows.Count; i++)
{
wbSubTotal += Convert.ToDecimal(gvStock.Rows[i].Cells[gvStock.Rows[i].Cells.Count - 1].Text);
bSubTotal += Convert.ToDecimal(gvStock.Rows[i].Cells[gvStock.Rows[i].Cells.Count - 2].Text);
}
this.AddTotalRow("Sub Total", bSubTotal.ToString("N2"), wbSubTotal.ToString("N2"));
this.AddTotalRow("Total", balanceTotal.ToString("N2"), WBalanceTotal.ToString("N2"));
for (int i = gvStock.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = gvStock.Rows[i];
GridViewRow previousRow = gvStock.Rows[i - 1];
for (int j = 0; j < row.Cells.Count; j++)
{
if (row.Cells[j].Text == previousRow.Cells[j].Text && row.Cells[j].Text != "0")
{
if (previousRow.Cells[j].RowSpan == 0)
{
if (row.Cells[j].RowSpan == 0)
{
previousRow.Cells[j].RowSpan += 2;
}
else
{
previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
}
row.Cells[j].Visible = false;
}
}
}
}
}
private void AddTotalRow(string labelText, string balance, string wBalance)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
row.Cells.AddRange(new TableCell[8] {
new TableCell (), //Empty Cell
new TableCell (), //Empty Cell
new TableCell (), //Empty Cell
new TableCell (), //Empty Cell
new TableCell (), //Empty Cell
new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right },
new TableCell { Text = balance, HorizontalAlign = HorizontalAlign.Right },
new TableCell { Text = wBalance, HorizontalAlign = HorizontalAlign.Right } });
gvStock.Controls[0].Controls.Add(row);
}
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("Category"), New DataColumn("Name"), New DataColumn("Product"), New DataColumn("Weight"), New DataColumn("Dispatch_Qty"), New DataColumn("Dispatch_Weight"), New DataColumn("Balance"), New DataColumn("W_Balance")})
dt.Rows.Add("India(Uncut)", "High Nech LS", "7", "7310", "1", "1047", "6", "6263")
dt.Rows.Add("India(Uncut)", "Printed Fleece Bootom", "27", "18062", "18", "11610", "9", "4652")
dt.Rows.Add("India(Uncut)", "Uncut Cotton Jeans 100%", "1", "119", "1", "1119", "0", "0")
dt.Rows.Add("India(Uncut)", "Uncut Jeans", "240", "295265", "180", " 221058", "60", "74204")
dt.Rows.Add("India(Uncut)", "Uncut Sweater", "591", "582445", "467", "462902", "124", "119543")
dt.Rows.Add("India(Uncut)", "Uncut White Ploy", "30", "31960", "27", "28684", "3", "3276")
dt.Rows.Add("India(Uncut)", "Uncut Wool Body", "63", "57419", "63", " 57419", "0", "0")
dt.Rows.Add("India(Uncut)", "Uncut Wool Pants", "40", "43372", "40", " 43372", "0", "0")
dt.Rows.Add("India(Uncut)", "Uncut Wool Sweaters 50-100%", "2", "200", "2", "200", "0", "0")
dt.Rows.Add("India(Uncut)", "Uncut Wool Sweaters 60-100%", "26", "18071", "26", "18071", "0", "0")
dt.Rows.Add("India(Uncut)", "White Socks", "37", "31647", "36", " 30778", "1", "869")
dt.Rows.Add("India(Cut)", "High Nech LS", "327", "7310", "1", "1047", "6", "6263")
dt.Rows.Add("India(Cut)", "Printed Fleece Bootom", "146", "18062", "18", "11610", "9", "4652")
dt.Rows.Add("India(Cut)", "Cut Cotton Jeans 100%", "615", "119", "1", "1119", "0", "0")
dt.Rows.Add("India(Cut)", "Cut Jeans", "34", "295265", "180", " 221058", "60", "74204")
dt.Rows.Add("India(Cut)", "Cut Sweater", "106", "582445", "467", "462902", "124", "119543")
dt.Rows.Add("India(Cut)", "Cut White Ploy", "37", "31960", "27", "28684", "3", "3276")
dt.Rows.Add("India(Cut)", "Cut Wool Body", "106", "100218", "97", "91539", "9", "8679")
dt.Rows.Add("India(Cut)", "Cut Wool Pants", "37", "41179", "35", "39045", "0", "0")
dt.Rows.Add("India(Cut)", "Cut Wool Sweaters 50-100%", "1", "500", "1", "500", "0", "0")
dt.Rows.Add("India(Cut)", "Cut Wool Sweaters 60-100%", "57", "40870", "57", "40870", "0", "0")
Me.gvStock.DataSource = dt
Me.gvStock.DataBind()
End If
End Sub
Private currentCategory As String = ""
Private wbSubTotal As Decimal = 0
Private bSubTotal As Decimal = 0
Private WBalanceTotal As Decimal = 0
Private balanceTotal As Decimal = 0
Private rowIndex As Integer = 0
Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
wbSubTotal = 0
bSubTotal = 0
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dt As DataTable = (TryCast(e.Row.DataItem, DataRowView)).DataView.Table
Dim category As String = dt.Rows(e.Row.RowIndex)("Category").ToString()
balanceTotal += Convert.ToDecimal(dt.Rows(e.Row.RowIndex)("Balance"))
WBalanceTotal += Convert.ToDecimal(dt.Rows(e.Row.RowIndex)("W_Balance"))
If category <> currentCategory Then
If e.Row.RowIndex > 0 Then
For i As Integer = rowIndex To e.Row.RowIndex - 1
wbSubTotal += Convert.ToDecimal(gvStock.Rows(i).Cells(e.Row.Cells.Count - 1).Text)
bSubTotal += Convert.ToDecimal(gvStock.Rows(i).Cells(e.Row.Cells.Count - 2).Text)
Next
Me.AddTotalRow("Sub Total", bSubTotal.ToString("N2"), wbSubTotal.ToString("N2"))
rowIndex = e.Row.RowIndex
End If
currentCategory = category
End If
End If
End Sub
Protected Sub gvStock_DataBound(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = rowIndex To gvStock.Rows.Count - 1
wbSubTotal += Convert.ToDecimal(gvStock.Rows(i).Cells(gvStock.Rows(i).Cells.Count - 1).Text)
bSubTotal += Convert.ToDecimal(gvStock.Rows(i).Cells(gvStock.Rows(i).Cells.Count - 2).Text)
Next
Me.AddTotalRow("Sub Total", bSubTotal.ToString("N2"), wbSubTotal.ToString("N2"))
Me.AddTotalRow("Total", balanceTotal.ToString("N2"), WBalanceTotal.ToString("N2"))
For i As Integer = gvStock.Rows.Count - 1 To 0 + 1
Dim row As GridViewRow = gvStock.Rows(i)
Dim previousRow As GridViewRow = gvStock.Rows(i - 1)
For j As Integer = 0 To row.Cells.Count - 1
If row.Cells(j).Text = previousRow.Cells(j).Text AndAlso row.Cells(j).Text <> "0" Then
If previousRow.Cells(j).RowSpan = 0 Then
If row.Cells(j).RowSpan = 0 Then
previousRow.Cells(j).RowSpan += 2
Else
previousRow.Cells(j).RowSpan = row.Cells(j).RowSpan + 1
End If
row.Cells(j).Visible = False
End If
End If
Next
Next
End Sub
Private Sub AddTotalRow(ByVal labelText As String, ByVal balance As String, ByVal wBalance As String)
Dim row As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
row.BackColor = ColorTranslator.FromHtml("#F9F9F9")
row.Cells.AddRange(New TableCell(7) {New TableCell(), New TableCell(), New TableCell(), New TableCell(), New TableCell(), New TableCell With {
.Text = labelText,
.HorizontalAlign = HorizontalAlign.Right
}, New TableCell With {
.Text = balance,
.HorizontalAlign = HorizontalAlign.Right
}, New TableCell With {
.Text = wBalance,
.HorizontalAlign = HorizontalAlign.Right
}})
gvStock.Controls(0).Controls.Add(row)
End Sub
Screenshot