locked
Show Gridview Data In Nested Gridview Footer Label RRS feed

Answers

  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description and codes,I have created a demo.Just like this:

     <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
                DataKeyNames="OrderNo" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <img alt="" style="cursor: pointer" src="image/download.jpg" />
                            <asp:Panel ID="pnlOrders" runat="server" Style="display: none; text-align: center;">
                                <asp:GridView ID="gvOrders" HorizontalAlign="Center" runat="server" Width="750px" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound1" AutoGenerateColumns="false" CssClass="ChildGrid">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Order No">
                                            <ItemTemplate>
                                                <asp:Label ID="L1" runat="server" Width="50px" Text='<%# Bind("OrderNo") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
    
                                        <asp:TemplateField HeaderText="ItemName">
                                            <ItemTemplate>
                                                <asp:Label ID="L2" runat="server" Width="50px" Text='<%# Bind("ItemName") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Despatch Date">
                                            <ItemTemplate>
                                                <asp:Label ID="L3" runat="server" Width="50px" Text='<%# Bind("Despatch_Date") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
    
                                        <asp:TemplateField HeaderText="Despatch Qty">
                                            <ItemTemplate>
                                                <asp:Label ID="L4" runat="server" Width="50px" Text='<%# Bind("Dispatched_Quantity") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
    
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Order No">
                        <ItemTemplate>
                            <asp:Label ID="L1" runat="server" Width="50px" Text='<%# Bind("OrderNo") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ItemName">
                        <ItemTemplate>
                            <asp:Label ID="L22" runat="server" Width="50px" Text='<%# Bind("ItemName") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle BackColor="#A2D9CE" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Order Quantity">
                        <ItemTemplate>
                            <asp:Label ID="L33" runat="server" Width="100px" Text='<%# Bind("Order_Quantity") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="FL33" runat="server" Width="100px"></asp:Label>
                        </FooterTemplate>
                        <ItemStyle HorizontalAlign="Center" BackColor="#AAB7B8" />
                        <HeaderStyle HorizontalAlign="Center" BackColor="#AAB7B8" />
                        <FooterStyle HorizontalAlign="Center" BackColor="#AAB7B8" Font-Size="20px" Font-Bold="true" ForeColor="Black" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
     private DataTable BindGridview()
            {
                DataTable dt = new DataTable();
                SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                con1.Open();
                SqlCommand cmd1 = new SqlCommand("select OrderNo,Item_Name as ItemName,Order_Quantity from orders", con1);
                SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
                ada1.Fill(dt);
                return dt;
    
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dtz = BindGridview();
                    gvCustomers.DataSource = dtz;
                    gvCustomers.DataBind();
                    Session["ORSTRP"] = dtz;
                    DataView dView = new DataView(dtz);
                    Session["OSR"] = dView;
    
    
                    if (Session["ORSTRP"] != null)
                    {
                        D1.Items.Clear();
                        D1.AppendDataBoundItems = true;
                        D1.Items.Insert(0, new ListItem("Select Order No", "Select Order No"));
                        DataTable wab = (DataTable)Session["ORSTRP"];
                        DataTable wydtf = wab.DefaultView.ToTable(true, "OrderNo");
                        DataView wyzdView = new DataView(wydtf);
    
                        D1.DataSource = wyzdView;
                        D1.DataTextField = "OrderNo";
                        D1.DataValueField = "OrderNo";
                        D1.DataBind();
                    }
                }
            }
     protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
            {
                
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                    con.Open();
                    GridView gv = (GridView)e.Row.FindControl("gvOrders");
                    Label ON = (Label)e.Row.FindControl("L1");
                    Label IN = (Label)e.Row.FindControl("L22");
    
                    //  
    
    
                    SqlCommand cmd = new SqlCommand("select OrderNo,ItemName,Replace(CONVERT(VARCHAR(15), Despatch_Date, 106), ' ', '-') as Despatch_Date,Dispatched_Quantity from Dispatch where OrderNo='" + ON.Text + "' and ItemName='" + IN.Text + "'", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    con.Close();
                    gv.DataSource = ds;
                    gv.DataBind();
    
                }
            }
    
            protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            decimal TPcs = 0;
            decimal TPcs1 = 0;
            protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
            {
                Label lq = (Label)e.Row.Parent.Parent.Parent.FindControl("L33");
    
                GridView gv = (GridView)e.Row.Parent.Parent;
    
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    float t = 0, s = 0 ;
                    foreach (GridViewRow r in gv.Rows)
                    {
                        Label Despatch = (Label)r.FindControl("L4");
                        t += float.Parse(Despatch.Text);
                    }
                    s = t;
                    e.Row.Cells[0].Text = "TOTAL";
                    e.Row.Cells[1].Text = "Order Quantity="+lq.Text;
                    e.Row.Cells[2].Text = "Total Dispatched Quantity=" + s.ToString();
                    e.Row.Cells[3].Text = "BALANCE TO Dispatched=" + (Convert.ToInt32(lq.Text) - Convert.ToInt32(s));
                }
                
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 9, 2020 8:59 AM

All replies

  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description,I don't understand your requirment clearly.Do you post your sample to us?

    I have created a demo for you .Just like this:

     <script src="Scripts/jquery-3.1.1.min.js"></script>
        <script>
            $(function () {
                 $("[src*=download]").on("click", function () {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
                $(this).attr("src", "image/download.jpg");
            });
            $("[src*=minus]").on("click", function () {
                $(this).attr("src", "image/download.jpg");
                $(this).closest("tr").next().remove();
            });
            })
        </script>
    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
                DataKeyNames="Id" OnRowDataBound="gvCustomers_RowDataBound" ShowFooter="true">
                <Columns>
                    <asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" />
                    <asp:BoundField ItemStyle-Width="150px" DataField="num" HeaderText="num" />
                    <asp:TemplateField>
                        <FooterTemplate>
                            <img alt = "" style="cursor: pointer" src="image/download.jpg" />
                            <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                                <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                                    <Columns>
                                        <asp:BoundField ItemStyle-Width="150px" DataField="Total" HeaderText="Total" />
                                    </Columns>
                                </asp:GridView>
                            </asp:Panel>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    Code-behind:

     protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test");
                gvCustomers.DataSource = ds.Tables[0].DefaultView;
                gvCustomers.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
            protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    //string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
                    GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
                    gvOrders.DataSource = GetData(string.Format("select top 3 * from Test "));
                    gvOrders.DataBind();
                }
            }
            private static DataTable GetData(string query)
            {
               string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
    
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = query;
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataSet ds = new DataSet())
                            {
                                DataTable dt = new DataTable();
                                sda.Fill(dt);
                                return dt;
                            }
                        }
                    }
                }
            }

    Best-regards,

    Yijing Sun

    Monday, December 7, 2020 3:35 AM
  • User-807418713 posted

    Hello

    Here Is My Complete code please use this

    SQL Query

    USE [master]
    GO
    /****** Object:  Table [dbo].[Orders]    Script Date: 12/07/2020 20:24:12 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Orders](
    	[OrderNo] [varchar](50) NULL,
    	[Item_Name] [varchar](50) NULL,
    	[Order_Quantity] [float] NULL
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    INSERT [dbo].[Orders] ([OrderNo], [Item_Name], [Order_Quantity]) VALUES (N'01', N'AA', 100)
    INSERT [dbo].[Orders] ([OrderNo], [Item_Name], [Order_Quantity]) VALUES (N'01', N'BB', 200)
    INSERT [dbo].[Orders] ([OrderNo], [Item_Name], [Order_Quantity]) VALUES (N'N23', N'TT', 50)
    /****** Object:  Table [dbo].[Dispatch]    Script Date: 12/07/2020 20:24:12 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Dispatch](
    	[OrderNo] [varchar](50) NULL,
    	[ItemName] [varchar](150) NULL,
    	[Despatch_Date] [datetime] NULL,
    	[Dispatched_Quantity] [float] NULL
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    INSERT [dbo].[Dispatch] ([OrderNo], [ItemName], [Despatch_Date], [Dispatched_Quantity]) VALUES (N'01', N'AA', CAST(0x0000AC6600000000 AS DateTime), 10)
    INSERT [dbo].[Dispatch] ([OrderNo], [ItemName], [Despatch_Date], [Dispatched_Quantity]) VALUES (N'01', N'AA', CAST(0x0000AC6700000000 AS DateTime), 20)
    INSERT [dbo].[Dispatch] ([OrderNo], [ItemName], [Despatch_Date], [Dispatched_Quantity]) VALUES (N'N23', N'TT', CAST(0x0000AC6800000000 AS DateTime), 5)
    

    ASPX PAGE 

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="A.aspx.cs" Inherits="A" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        
        
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("[src*=plus]").live("click", function () {
            $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
            $(this).attr("src", "images/minus.png");
        });
        $("[src*=minus]").live("click", function () {
            $(this).attr("src", "images/plus.png");
            $(this).closest("tr").next().remove();
        });
    </script>
    
    
        <script src="Scripts/chosen.jquery.js" type="text/javascript"></script>
     	<link rel="stylesheet" href="Style/chosen.css" /> 
    		  <script type="text/javascript">
            $(function () {
                $(".chzn-select").chosen();
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
                function EndRequestHandler(sender, args) {
                    //Binding Code Again
                    $(".chzn-select").chosen();
                }
            });
        </script>
        
        <strong>Select Order No:-
            <asp:DropDownList ID="D1" runat="server">
            </asp:DropDownList></strong><br />
    
    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
        DataKeyNames="OrderNo"  OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <img alt = "" style="cursor: pointer" src="images/plus.png" />
                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none; text-align:center;">
                 
                        <asp:GridView ID="gvOrders" HorizontalAlign="Center" runat="server" Width="750px" ShowFooter="True"  OnRowDataBound="GridView1_RowDataBound1" AutoGenerateColumns="false" CssClass = "ChildGrid">
                            <Columns>
                            
                            
                              <asp:TemplateField HeaderText="Order No">
               <ItemTemplate>
                    <asp:Label ID="L1" runat="server" Width="50px" Text='<%# Bind("OrderNo") %>'></asp:Label>
               </ItemTemplate>
               <ItemStyle BackColor="#A2D9CE" />
               </asp:TemplateField>
               
                <asp:TemplateField HeaderText="ItemName">
               <ItemTemplate>
                    <asp:Label ID="L2" runat="server"  Width="50px" Text='<%# Bind("ItemName") %>'></asp:Label>
               </ItemTemplate>
                  <ItemStyle BackColor="#A2D9CE" />
               </asp:TemplateField>
               
               
             
                            
                                 <asp:TemplateField HeaderText="Despatch Date">
               <ItemTemplate>
                    <asp:Label ID="L3" runat="server"  Width="50px" Text='<%# Bind("Despatch_Date") %>'></asp:Label>
               </ItemTemplate>
                  <ItemStyle BackColor="#A2D9CE" />
               </asp:TemplateField>
                              
                                
                                 <asp:TemplateField HeaderText="Despatch Qty">
               <ItemTemplate>
                    <asp:Label ID="L4" runat="server"  Width="50px" Text='<%# Bind("Dispatched_Quantity") %>'></asp:Label>
               </ItemTemplate>
                  <ItemStyle BackColor="#A2D9CE" />
               </asp:TemplateField>
               
                            </Columns>
                        </asp:GridView>
                   
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
           
             
               
                <asp:TemplateField HeaderText="Order No">                            
                                <ItemTemplate>
                                    <asp:Label ID="L1" runat="server" Width="50px" Text='<%# Bind("OrderNo") %>'></asp:Label>
                                </ItemTemplate>
                                
                            </asp:TemplateField>
                                                       
                                  <asp:TemplateField HeaderText="ItemName">
               <ItemTemplate>
                    <asp:Label ID="L22" runat="server"  Width="50px" Text='<%# Bind("ItemName") %>'></asp:Label>
               </ItemTemplate>
                  <ItemStyle BackColor="#A2D9CE" />
               </asp:TemplateField>
                                
                                
                                 
                                
                                 <asp:TemplateField HeaderText="Order Quantity">
                                <ItemTemplate>
                                <asp:Label ID="L33" runat="server" Width="100px" Text='<%# Bind("Order_Quantity") %>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                 <asp:Label ID="FL33" runat="server" Width="100px"></asp:Label>
                                </FooterTemplate>
                                  <ItemStyle  HorizontalAlign="Center" BackColor="#AAB7B8"   />
                                   <HeaderStyle  HorizontalAlign="Center" BackColor="#AAB7B8"   />
                                    <FooterStyle  HorizontalAlign="Center" BackColor="#AAB7B8" Font-Size="20px" Font-Bold="true" ForeColor="Black"   />
                                </asp:TemplateField>
                                
               
             
               
               
             
        </Columns>
    </asp:GridView>
    </asp:Content>
    
    

    C# Page

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    using System.Globalization;
    
    public partial class A : System.Web.UI.Page
    {
       private DataTable BindGridview()
        {
            DataTable dt = new DataTable();
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
            con1.Open();
            SqlCommand cmd1 = new SqlCommand("select OrderNo,Item_Name as ItemName,Order_Quantity from orders", con1);
            SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
            ada1.Fill(dt);
            return dt;
            con1.Close();
    
        }
    
        
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dtz = BindGridview();
                gvCustomers.DataSource = dtz;
                gvCustomers.DataBind();
                Session["ORSTRP"] = dtz;
                DataView dView = new DataView(dtz);
                Session["OSR"] = dView;
    
    
                if (Session["ORSTRP"] != null)
                {
                    D1.Items.Clear();
                    D1.AppendDataBoundItems = true;
                    D1.Items.Insert(0, new ListItem("Select Order No", "Select Order No"));
                    DataTable wab = (DataTable)Session["ORSTRP"];
                    DataTable wydtf = wab.DefaultView.ToTable(true, "OrderNo");
                    DataView wyzdView = new DataView(wydtf);
                  
                    D1.DataSource = wyzdView;
                    D1.DataTextField = "OrderNo";
                    D1.DataValueField = "OrderNo";
                    D1.DataBind();
                }
    
               
            }
        }
    
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
                con.Open();
                GridView gv = (GridView)e.Row.FindControl("gvOrders");
                Label ON = (Label)e.Row.FindControl("L1");
                Label IN = (Label)e.Row.FindControl("L22");
     
              //  
    
    
                SqlCommand cmd = new SqlCommand("select OrderNo,ItemName,Replace(CONVERT(VARCHAR(15), Despatch_Date, 106), ' ', '-') as Despatch_Date,Dispatched_Quantity from Dispatch where OrderNo='" + ON.Text + "' and ItemName='"+IN.Text+"'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                con.Close();
                gv.DataSource = ds;
                gv.DataBind();
    
                
                //Label ddlCountries = gv.FooterRow.FindControl("FLL11") as Label;
                 
            }
        }
        decimal TPcs = 0;
        decimal TPcs1 = 0;
        protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
    
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    Label LPrice = (Label)e.Row.FindControl("L66");
            //    decimal PiecesTotal = Convert.ToDecimal(LPrice.Text);
    
            //    if (e.Row.RowIndex == 0)
            //    {
            //        TPcs = PiecesTotal;
            //    }
            //    else
            //    {
            //        TPcs += PiecesTotal;
            //    }
            //    LPrice.Text = PiecesTotal.ToString();
    
                 
            //}
    
    
            //if (e.Row.RowType == DataControlRowType.Footer)
            //{
    
    
            //    Label LblPieces = (Label)e.Row.FindControl("FL66");
            //    LblPieces.Text = TPcs.ToString();
    
     
            //}
    
    
        }
        protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
        protected void D1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)Session["ORSTRP"];
            DataView dView = new DataView(dt);
            dView.RowFilter = "MPONo = '" + D1.Text + "'";
            gvCustomers.DataSource = dView;
            gvCustomers.DataBind();
            Session["ORSTRP"] = dView;
        }
        protected void refresh_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
     
    }
    

    Result Set Showing Like this Below

    I want Like This as below WITH TOTAL

    HOW TO DO PLEASE GIVE ME CODE

    Monday, December 7, 2020 3:01 PM
  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description and codes,I have created a demo.Just like this:

     <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
                DataKeyNames="OrderNo" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <img alt="" style="cursor: pointer" src="image/download.jpg" />
                            <asp:Panel ID="pnlOrders" runat="server" Style="display: none; text-align: center;">
                                <asp:GridView ID="gvOrders" HorizontalAlign="Center" runat="server" Width="750px" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound1" AutoGenerateColumns="false" CssClass="ChildGrid">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Order No">
                                            <ItemTemplate>
                                                <asp:Label ID="L1" runat="server" Width="50px" Text='<%# Bind("OrderNo") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
    
                                        <asp:TemplateField HeaderText="ItemName">
                                            <ItemTemplate>
                                                <asp:Label ID="L2" runat="server" Width="50px" Text='<%# Bind("ItemName") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Despatch Date">
                                            <ItemTemplate>
                                                <asp:Label ID="L3" runat="server" Width="50px" Text='<%# Bind("Despatch_Date") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
    
                                        <asp:TemplateField HeaderText="Despatch Qty">
                                            <ItemTemplate>
                                                <asp:Label ID="L4" runat="server" Width="50px" Text='<%# Bind("Dispatched_Quantity") %>'></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle BackColor="#A2D9CE" />
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
    
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Order No">
                        <ItemTemplate>
                            <asp:Label ID="L1" runat="server" Width="50px" Text='<%# Bind("OrderNo") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ItemName">
                        <ItemTemplate>
                            <asp:Label ID="L22" runat="server" Width="50px" Text='<%# Bind("ItemName") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle BackColor="#A2D9CE" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Order Quantity">
                        <ItemTemplate>
                            <asp:Label ID="L33" runat="server" Width="100px" Text='<%# Bind("Order_Quantity") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="FL33" runat="server" Width="100px"></asp:Label>
                        </FooterTemplate>
                        <ItemStyle HorizontalAlign="Center" BackColor="#AAB7B8" />
                        <HeaderStyle HorizontalAlign="Center" BackColor="#AAB7B8" />
                        <FooterStyle HorizontalAlign="Center" BackColor="#AAB7B8" Font-Size="20px" Font-Bold="true" ForeColor="Black" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
     private DataTable BindGridview()
            {
                DataTable dt = new DataTable();
                SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                con1.Open();
                SqlCommand cmd1 = new SqlCommand("select OrderNo,Item_Name as ItemName,Order_Quantity from orders", con1);
                SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
                ada1.Fill(dt);
                return dt;
    
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dtz = BindGridview();
                    gvCustomers.DataSource = dtz;
                    gvCustomers.DataBind();
                    Session["ORSTRP"] = dtz;
                    DataView dView = new DataView(dtz);
                    Session["OSR"] = dView;
    
    
                    if (Session["ORSTRP"] != null)
                    {
                        D1.Items.Clear();
                        D1.AppendDataBoundItems = true;
                        D1.Items.Insert(0, new ListItem("Select Order No", "Select Order No"));
                        DataTable wab = (DataTable)Session["ORSTRP"];
                        DataTable wydtf = wab.DefaultView.ToTable(true, "OrderNo");
                        DataView wyzdView = new DataView(wydtf);
    
                        D1.DataSource = wyzdView;
                        D1.DataTextField = "OrderNo";
                        D1.DataValueField = "OrderNo";
                        D1.DataBind();
                    }
                }
            }
     protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
            {
                
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                    con.Open();
                    GridView gv = (GridView)e.Row.FindControl("gvOrders");
                    Label ON = (Label)e.Row.FindControl("L1");
                    Label IN = (Label)e.Row.FindControl("L22");
    
                    //  
    
    
                    SqlCommand cmd = new SqlCommand("select OrderNo,ItemName,Replace(CONVERT(VARCHAR(15), Despatch_Date, 106), ' ', '-') as Despatch_Date,Dispatched_Quantity from Dispatch where OrderNo='" + ON.Text + "' and ItemName='" + IN.Text + "'", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    con.Close();
                    gv.DataSource = ds;
                    gv.DataBind();
    
                }
            }
    
            protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            decimal TPcs = 0;
            decimal TPcs1 = 0;
            protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
            {
                Label lq = (Label)e.Row.Parent.Parent.Parent.FindControl("L33");
    
                GridView gv = (GridView)e.Row.Parent.Parent;
    
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    float t = 0, s = 0 ;
                    foreach (GridViewRow r in gv.Rows)
                    {
                        Label Despatch = (Label)r.FindControl("L4");
                        t += float.Parse(Despatch.Text);
                    }
                    s = t;
                    e.Row.Cells[0].Text = "TOTAL";
                    e.Row.Cells[1].Text = "Order Quantity="+lq.Text;
                    e.Row.Cells[2].Text = "Total Dispatched Quantity=" + s.ToString();
                    e.Row.Cells[3].Text = "BALANCE TO Dispatched=" + (Convert.ToInt32(lq.Text) - Convert.ToInt32(s));
                }
                
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 9, 2020 8:59 AM