publicstring constr =ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();protectedvoidPage_Load(object sender,EventArgs e){if(!IsPostBack){BindGridView();}}protectedvoidBindGridView(){DataTable dt =newDataTable();using(SqlConnection con =newSqlConnection(constr)){string cmdtext ="select ID, CustomerName, ItemName, Price, Qty from TestTable2";using(SqlCommand cmd =newSqlCommand(cmdtext, con)){
con.Open();SqlDataAdapter sda =newSqlDataAdapter(cmd);
sda.Fill(dt);GridView1.DataSource= dt;GridView1.DataBind();}}}// 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 double dblSubTotalUnitPrice =0;double dblSubTotalQuantity =0;// To temporarily store Grand Total double dblGrandTotalUnitPrice =0;double dblGrandTotalQuantity =0;protectedvoidGridView1_RowCreated(object sender,GridViewRowEventArgs e){boolIsSubTotalRowNeedToAdd=false;boolIsGrandTotalRowNeedtoAdd=false;if((strPreviousRowName !=string.Empty)&&(DataBinder.Eval(e.Row.DataItem,"CustomerName")!=null))if(strPreviousRowName !=DataBinder.Eval(e.Row.DataItem,"CustomerName").ToString())IsSubTotalRowNeedToAdd=true;if((strPreviousRowName !=string.Empty)&&(DataBinder.Eval(e.Row.DataItem,"CustomerName")==null)){IsSubTotalRowNeedToAdd=true;IsGrandTotalRowNeedtoAdd=true;
intSubTotalIndex =0;}if((strPreviousRowName ==string.Empty)&&(DataBinder.Eval(e.Row.DataItem,"CustomerName")!=null)){GridView grdViewOrders =(GridView)sender;GridViewRow row =newGridViewRow(0,0,DataControlRowType.DataRow,DataControlRowState.Insert);TableCell cell =newTableCell();
cell.Text="Customer Name : "+DataBinder.Eval(e.Row.DataItem,"CustomerName").ToString();
cell.ColumnSpan=3;
cell.CssClass="GroupHeaderStyle";
row.Cells.Add(cell);
grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex+ intSubTotalIndex, row);
intSubTotalIndex++;}if(IsSubTotalRowNeedToAdd){#region Adding Sub Total RowGridView grdViewOrders =(GridView)sender;// Creating a Row GridViewRow row =newGridViewRow(0,0,DataControlRowType.DataRow,DataControlRowState.Insert);//Adding Total Cell TableCell cell =newTableCell();
cell.Text="Sub Total";
cell.HorizontalAlign=HorizontalAlign.Left;
cell.ColumnSpan=1;
cell.CssClass="SubTotalRowStyle";
row.Cells.Add(cell);//Adding Quantity Column
cell =newTableCell();
cell.Text= dblSubTotalQuantity.ToString();
cell.HorizontalAlign=HorizontalAlign.Right;
cell.CssClass="SubTotalRowStyle";
row.Cells.Add(cell);//Adding Unit Price Column
cell =newTableCell();
cell.Text=string.Format("{0:0.00}", dblSubTotalUnitPrice);
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++;#endregion#region Adding Next Group Header Detailsif(DataBinder.Eval(e.Row.DataItem,"CustomerName")!=null){
row =newGridViewRow(0,0,DataControlRowType.DataRow,DataControlRowState.Insert);
cell =newTableCell();
cell.Text="Customer Name : "+DataBinder.Eval(e.Row.DataItem,"CustomerName").ToString();
cell.ColumnSpan=3;
cell.CssClass="GroupHeaderStyle";
row.Cells.Add(cell);
grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex+ intSubTotalIndex, row);
intSubTotalIndex++;}#endregion#region Reseting the Sub Total Variables
dblSubTotalUnitPrice =0;
dblSubTotalQuantity =0;#endregion}if(IsGrandTotalRowNeedtoAdd){#region Grand Total RowGridView grdViewOrders =(GridView)sender;// Creating a Row GridViewRow row =newGridViewRow(0,0,DataControlRowType.DataRow,DataControlRowState.Insert);//Adding Total Cell TableCell cell =newTableCell();
cell.Text="Grand Total";
cell.HorizontalAlign=HorizontalAlign.Left;
cell.ColumnSpan=1;
cell.CssClass="GrandTotalRowStyle";
row.Cells.Add(cell);//Adding Quantity Column
cell =newTableCell();
cell.Text= dblGrandTotalQuantity.ToString();
cell.HorizontalAlign=HorizontalAlign.Right;
cell.CssClass="GrandTotalRowStyle";
row.Cells.Add(cell);//Adding Unit Price Column
cell =newTableCell();
cell.Text=string.Format("{0:0.00}", dblGrandTotalUnitPrice);
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);#endregion}}protectedvoidGridView1_RowDataBound(object sender,GridViewRowEventArgs e){// This is for cumulating the values if(e.Row.RowType==DataControlRowType.DataRow){
strPreviousRowName =DataBinder.Eval(e.Row.DataItem,"CustomerName").ToString();double dblUnitPrice =Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Price").ToString());double dblQuantity =Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Qty").ToString());// Cumulating Sub Total
dblSubTotalUnitPrice += dblUnitPrice;
dblSubTotalQuantity += dblQuantity;// Cumulating Grand Total
dblGrandTotalUnitPrice += dblUnitPrice;
dblGrandTotalQuantity += dblQuantity;}}
I want one grand header that is RefNo and then sub header and details
as of now im getting header customername and details
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
399 Points
1101 Posts
Need One Header then sub header then details
Aug 02, 2017 12:15 PM|asp.ambur|LINK
Hello
This is my code
I want one grand header that is RefNo and then sub header and details
as of now im getting header customername and details
Contributor
6730 Points
2715 Posts
Re: Need One Header then sub header then details
Aug 03, 2017 07:28 AM|Eric Du|LINK
Hi asp.ambur,
According to your description, as far as I know, if you want to add one header of the gridview, please check the following sample(OnDataBound event):
Sample Code:
<head runat="server"> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363" runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound"> <Columns> <asp:BoundField DataField="CustomerName" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="CustomerCountry" HeaderText="Country" ItemStyle-Width="150" /> <asp:BoundField DataField="EmployeeName" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="EmployeeCountry" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> </form> </body> protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[4] { new DataColumn("CustomerName"), new DataColumn("CustomerCountry"), new DataColumn("EmployeeName"), new DataColumn("EmployeeCountry") }); dt.Rows.Add("John Hammond", "United States", "Albert Dunner", "Bolivia"); dt.Rows.Add("Mudassar Khan", "India", "Jason Sprint", "Canada"); dt.Rows.Add("Suzanne Mathews", "France", "Alfred Lobo", "Philippines"); dt.Rows.Add("Robert Schidner", "Russia", "Shaikh Ayyaz", "UAE"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void OnDataBound(object sender, EventArgs e) { GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal); TableHeaderCell cell = new TableHeaderCell(); cell.Text = "Customers"; cell.ColumnSpan = 2; row.Controls.Add(cell); cell = new TableHeaderCell(); cell.ColumnSpan = 2; cell.Text = "Employees"; row.Controls.Add(cell); row.BackColor = ColorTranslator.FromHtml("#3AC0F2"); GridView1.HeaderRow.Parent.Controls.AddAt(0, row); }
Result:
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
399 Points
1101 Posts
Re: Need One Header then sub header then details
Aug 03, 2017 07:43 AM|asp.ambur|LINK
Hello Eric
Thanks For Your Code
Here is my complete code im using this format
https://forums.asp.net/t/2125642.aspx?Merge+Gridview+Total+and+Grand+Total
everything fine except i want one more top header that is RefNo to show..
How to show RefNo using my existing code
https://forums.asp.net/t/2125642.aspx?Merge+Gridview+Total+and+Grand+Total
Thanking You