locked
Need One Header then sub header then details RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my code

    public string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }
    
            protected void BindGridView()
            {
                DataTable dt = new DataTable();
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string cmdtext = "select ID, CustomerName, ItemName, Price, Qty from TestTable2";
                    using (SqlCommand cmd = new SqlCommand(cmdtext, con))
                    {
                        con.Open();
                        SqlDataAdapter sda = new SqlDataAdapter(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;
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                bool IsSubTotalRowNeedToAdd = false;
                bool IsGrandTotalRowNeedtoAdd = 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 = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                    TableCell cell = new TableCell();
                    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 Row
                    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 = "Sub Total";
                    cell.HorizontalAlign = HorizontalAlign.Left;
                    cell.ColumnSpan = 1;
                    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 = 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 Details
                    if (DataBinder.Eval(e.Row.DataItem, "CustomerName") != null)
                    {
                        row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                        cell = new TableCell();
                        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 Row
                    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.ColumnSpan = 1;
                    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 = 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
                }
            }
            
    
            protected void GridView1_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

    Wednesday, August 2, 2017 12:15 PM

Answers

  • User-1838255255 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 3, 2017 7:28 AM

All replies

  • User-1838255255 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 3, 2017 7:28 AM
  • User2033107836 posted

    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

    Thursday, August 3, 2017 7:43 AM