Hi @RAVI ,
I tried what you said. The reason it doesn't work is because the header column count doesn't match, the header column count and row column count should be equal.
You just need to add a column to GrandTotal.
cell = new TableCell();
cell.Text = "---";
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item1", typeof(string)),
new DataColumn("Description",typeof(string)),
new DataColumn("Qty", typeof(int)),
new DataColumn("Value", typeof(int))});
dt.Rows.Add("A", "M1", "2", "1");
dt.Rows.Add("A", "M2", "4", "2");
dt.Rows.Add("A", "M3", "6", "4");
dt.Rows.Add("E", "N4", "3", "4");
dt.Rows.Add("E", "N4", "6", "10");
dt.Rows.Add("F", "P1", "2", "20");
dt.Rows.Add("F", "K1", "3", "10");
dt.Rows.Add("F", "Z1", "5", "20");
gvDemo.DataSource = dt;
gvDemo.DataBind();
gvDemo.UseAccessibleHeader = true;
gvDemo.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
// To keep track of the previous row Group Identifier
string strPreviousRowName = string.Empty;
// To keep track the Index of Group Total
int intSubTotalIndex = 1;
// To temporarily store Sub Total
string dblSubTotalDescription = "";
double dblSubTotalValue = 0;
double dblSubTotalQuantity = 0;
// To temporarily store Grand Total
double dblGrandTotalValue = 0;
double dblGrandTotalQuantity = 0;
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
bool IsSubTotalRowNeedToAdd = false;
bool IsGrandTotalRowNeedtoAdd = false;
if ((strPreviousRowName != string.Empty) && (DataBinder.Ev al(e.Row.DataItem, "Item1") != null))
if (strPreviousRowName != DataBinder.Ev al(e.Row.DataItem, "Item1").ToString())
IsSubTotalRowNeedToAdd = true;
if ((strPreviousRowName != string.Empty) && (DataBinder.Ev al(e.Row.DataItem, "Item1") == null))
{
IsSubTotalRowNeedToAdd = true;
IsGrandTotalRowNeedtoAdd = true;
intSubTotalIndex = 0;
}
if (IsSubTotalRowNeedToAdd)
{
GridView grdViewOrders = (GridView)sender;
// Creating a Row
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
//Adding Total Cell
TableCell cell = new TableCell();
cell.Text = strPreviousRowName.ToString();
cell.HorizontalAlign = HorizontalAlign.Left;
//cell.ColumnSpan = 2;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Create Unit Column
cell = new TableCell();
cell.Text = "Sub Total";
cell.HorizontalAlign = HorizontalAlign.Left;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Quantity Column
cell = new TableCell();
cell.Text = dblSubTotalQuantity.ToString();
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding Unit Price Column
cell = new TableCell();
cell.Text = dblSubTotalValue.ToString();
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "SubTotalRowStyle";
row.Cells.Add(cell);
//Adding the Row at the RowIndex position in the Grid
grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);
intSubTotalIndex++;
dblSubTotalDescription = "";
dblSubTotalValue = 0;
dblSubTotalQuantity = 0;
}
if (IsGrandTotalRowNeedtoAdd)
{
GridView grdViewOrders = (GridView)sender;
// Creating a Row
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
//Adding Total Cell
TableCell cell = new TableCell();
cell.Text = "Grand Total";
cell.HorizontalAlign = HorizontalAlign.Left;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = "---";
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Quantity Column
cell = new TableCell();
cell.Text = dblGrandTotalQuantity.ToString();
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding Unit Price Column
cell = new TableCell();
cell.Text = dblGrandTotalValue.ToString();
cell.HorizontalAlign = HorizontalAlign.Right;
cell.CssClass = "GrandTotalRowStyle";
row.Cells.Add(cell);
//Adding the Row at the RowIndex position in the Grid
grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex, row);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// This is for cumulating the values
if (e.Row.RowType == DataControlRowType.DataRow)
{
strPreviousRowName = DataBinder.Ev al(e.Row.DataItem, "Item1").ToString();
string dblDescription = Convert.ToString(DataBinder.Ev al(e.Row.DataItem, "Description").ToString());
double dblValue = Convert.ToDouble(DataBinder.Ev al(e.Row.DataItem, "Value").ToString());
double dblQuantity = Convert.ToDouble(DataBinder.Ev al(e.Row.DataItem, "Qty").ToString());
// Cumulating Sub Total
dblSubTotalDescription = dblDescription;
dblSubTotalValue += dblValue;
dblSubTotalQuantity += dblQuantity;
// Cumulating Grand Total
dblGrandTotalValue += dblValue;
dblGrandTotalQuantity += dblQuantity;
}
}
Only part of the code has been modified in the front end:
<style>
.SubTotalRowStyle {
border: solid 1px Black;
background-color: #D8D8D8;
font-weight: bold;
}
.GrandTotalRowStyle {
border: solid 1px Gray;
background-color: #000000;
color: #ffffff;
font-weight: bold;
}
.GroupHeaderStyle {
border: solid 1px Black;
background-color: #4682B4;
color: #ffffff;
font-weight: bold;
}
.serh-grid {
width: 85%;
border: 1px solid #6AB5FF;
background: #fff;
line-height: 14px;
font-size: 11px;
font-family: Verdana;
}
</style>
<asp:GridView ID="gvDemo" runat="server" AllowSorting="true" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"
OnRowCreated="GridView1_RowCreated" Font-Names="Calibri" Font-Size="11pt">
<asp:BoundField DataField="VALUE" HeaderText="VALUE" />
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.