locked
Summary(sum) of QTYBWeight if item is same in another gridview2 RRS feed

  • Question

  • User-367318540 posted

    i am inserting data into gridview1,now i want summary of all item in another gridview2 on same page ,below is my data

    ,Note:when i delete item from main gridview1 ,then in summary gridview2 item get less/delete.

    here is image for your consideration https://ibb.co/PMFP7gf

    i have done it but getting stuck when i delete item from gridview1 then it does not get less from gridview2

    below is my Data.

    Create table #item (Codeitem int,Descriptionitem varchar(max))
    create table #bigbale(BID int,Codeitem int,QTY int,Bweight int)
     
    insert into #item  values
    (1001, 'A'),
    (1002, 'B'),
    (1003, 'C'),
    (1004, 'D'),
    (1005, 'E'),
    (1006, 'F'),
    (1007, 'G')
      
    insert into #bigbale values
    (1,1001, 1,100),
    (2,1002, 1,100),
    (3,1003, 1,100),
    (4,1004, 1,100),
    (5,1005, 1,100),
    (6,1006, 1,100),
    (7,1007, 1,100),
    (8,1001, 1,100),
    (9,1004, 1,100),
    (10,1007, 1,100)
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BigbaleDispatch.aspx.cs" Inherits="WebApplication1.BigbaleDispatch" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
    
        <title>Dispath Bigbale</title>
        <script type="text/javascript">
        $(function () {
        
    
            //Calculate and update Grand Total.
            var grandTotal = 0;
            $("[id*=SBWeight]").each(function () {
                grandTotal = grandTotal + parseFloat($(this).html());
            });
            $("[id*=txtTotal]").val(grandTotal.toString());
        });
    
    
    
    
           
    </script>
        <style type="text/css">
            .auto-style1 {
                text-align: center;
            }
            .auto-style2 {
                width: 100%;
            }
            .auto-style3 {
                width: 352px;
            }
            .auto-style4 {
                text-align: left;
                width: 489px;
            }
            .auto-style5 {
                width: 352px;
                text-align: right;
            }
            .auto-style6 {
                width: 489px;
            }
            .auto-style7 {
                width: 352px;
                text-align: right;
                height: 33px;
            }
            .auto-style8 {
                width: 489px;
                height: 33px;
            }
            .auto-style9 {
                height: 33px;
            }
        </style>
    
    </head>
        
    <body>
        <form id="form1" runat="server">
            
            <div class="auto-style1">
                <h1><strong>BigBale Dispatch&nbsp;&nbsp;&nbsp;&nbsp; </strong></h1>
    
              </div>
            <table class="auto-style2">
                <tr>
                    <td class="auto-style5">Customer Name : </td>
                    <td class="auto-style4">
                        <asp:DropDownList ID="DDLcus" runat="server">
                        </asp:DropDownList>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Date :</td>
                    <td>
                        <asp:TextBox ID="txtEntrydate" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style6">&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style7">Bale No Input : </td>
                    <td class="auto-style8">
                        <asp:TextBox ID="txtsearcbn" AutoPostBack="true" runat="server" OnTextChanged="txtsearcbn_TextChanged" Height="24px" Width="127px"></asp:TextBox>
                        <asp:DropDownList ID="DDLitem" runat="server">
                        </asp:DropDownList>
                        <asp:TextBox ID="txtqty" runat="server"  Readonly="true" Height="16px" Width="18px"></asp:TextBox>
                                            <asp:TextBox ID="txtwt" runat="server"  Readonly="true" Height="16px" Width="18px"></asp:TextBox>
                        
                        <asp:Button ID="btnadd" runat="server" Text="ADD" Visible="false" OnClick="btnadd_Click" />
                    </td>
                    <td class="auto-style9"></td>
                </tr>
                <tr>
                    <td class="auto-style5">&nbsp;</td>
                    <td class="auto-style6">
                        &nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style5">&nbsp;</td>
                    <td class="auto-style6">
                        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"  />
                    </td>
                    <td>&nbsp; Summary &nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style5">&nbsp;</td>
                    <td class="auto-style6">
                        <asp:GridView ID="GVbal" runat="server" HeaderStyle-BackColor="#66ccff" HeaderStyle-ForeColor="White"
        CssClass="mygrdContent" PagerStyle-CssClass="pager" OnRowDeleting="GVbal_RowDeleting" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AutoGenerateColumns="false" >
                             <Columns>
             <asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
                <HeaderStyle CssClass="table_04" HorizontalAlign="Left"></HeaderStyle>
                <ItemStyle CssClass="table_02" HorizontalAlign="Left"></ItemStyle>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Bale_No">
                                    <ItemTemplate>
                                        <asp:Label ID="BID" runat="server" Text='<%#Bind("BID")%>'></asp:Label>
                                    </ItemTemplate>
                 </asp:TemplateField>
    
    
             <asp:TemplateField HeaderText="Description">
                                    <ItemTemplate>
                                        <asp:Label ID="Descriptionitem" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
                                    </ItemTemplate>
                 </asp:TemplateField>
    
                 <asp:TemplateField HeaderText="QTY" >
                                    <ItemTemplate>
                                        <asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
                                    </ItemTemplate>
                 </asp:TemplateField>
                      <asp:TemplateField HeaderText="Weight" >
                                    <ItemTemplate>
                                        <asp:Label ID="BWeight" runat="server" Text='<%#Bind("BWeight")%>'></asp:Label>
                                    </ItemTemplate>
                 </asp:TemplateField>
            
            <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
        </Columns>
                        </asp:GridView>
                    </td>
                    <td>
                        <asp:GridView ID="GVbalS" runat="server" HeaderStyle-BackColor="#66ccff" HeaderStyle-ForeColor="White"
        CssClass="mygrdContent" PagerStyle-CssClass="pager" ShowFooter="True" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AutoGenerateColumns="false" OnRowDataBound="GVbalS_RowDataBound"  >
                             <Columns>
             <asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
                <HeaderStyle CssClass="table_04" HorizontalAlign="Left"></HeaderStyle>
                <ItemStyle CssClass="table_02" HorizontalAlign="Left"></ItemStyle>
            </asp:TemplateField>
     
    
    
             <asp:TemplateField HeaderText="Description">
                                    <ItemTemplate>
                                        <asp:Label ID="SDescriptionitem" runat="server" Text='<%#Bind("SDescriptionitem")%>'></asp:Label>
                                    </ItemTemplate>
                 </asp:TemplateField>
    
                 <asp:TemplateField HeaderText="QTY" >
                                    <ItemTemplate>
                                        <asp:Label ID="SQTY" runat="server" Text='<%#Bind("SQTY")%>'></asp:Label>
                                    </ItemTemplate>
                        <FooterTemplate><asp:Label ID="lblTotal1" runat="server"></asp:Label></FooterTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField HeaderText="Weight" >
                                    <ItemTemplate>
                                        <asp:Label ID="SBWeight" runat="server" Text='<%#Bind("SBWeight")%>'></asp:Label>
                                    </ItemTemplate>
                       <FooterTemplate><asp:Label ID="lblTotal" runat="server"></asp:Label></FooterTemplate>
                 </asp:TemplateField>
            
      
        </Columns>
                        </asp:GridView>
    
                        <br />
    
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    

    c# insert gridview1 code 

            protected void btnadd_Click(object sender, EventArgs e)
            {
                insertbalgv();
             
            }
     private void insertbalgv()
            {
    
                if (DDLitem.SelectedItem != null)
                {
                    int codeitem = Convert.ToInt32(DDLitem.SelectedItem.Value.ToString());
                    string Descriptionitem = DDLitem.SelectedItem.Text.ToString();
    
                    DataTable dtgridview = ViewState["dt"] as DataTable;
                    DataRow dr2 = dtgridview.NewRow();
                    bool ifExist = false;
    
                    // foreach (GridViewRow row in GVbal.Rows)
                    foreach (GridViewRow dr in GVbal.Rows)
                    {
                        string bId = dr.Cells[0].Text; // If BoundField pass the cell value.
                                                       //string bId = (dr.FindControl("lblBID") as Label).Text; // If TemplateField use FindControl.
                        if (bId == txtsearcbn.Text.Trim())
                        {
                            ifExist = true;
                            break;
                        }
                    }
                    if (!ifExist)
                    {
                        dr2["BID"] = txtsearcbn.Text.Trim();
                        dr2["Codeitem"] = codeitem;
                        dr2["Descriptionitem"] = Descriptionitem;
                        dr2["QTY"] = txtqty.Text;
                        dr2["BWeight"] = txtwt.Text;
    
                        dtgridview.Rows.Add(dr2);
                        ViewState["dt"] = dtgridview;
                        GVbal.DataSource = dtgridview;
                        GVbal.DataBind();
                    }
                    else
                    {
                        // this.lbgvck.Visible = true;
                        Response.Write("<script language='javascript'>alert('Bale Already Added');</script>");
                    }
    
                    clear();
                }
            }
    
            private void clear()
            {
                txtsearcbn.Text = "";
            }

    Tuesday, November 12, 2019 3:58 PM

Answers

  • User288213138 posted

    Hi akhterr,

    here is image for your consideration https://ibb.co/PMFP7gf

    I found that the data in your image is different from the data you posted. and what is ViewState["dt"] in the insertbalgv method?

    $("[id*=txtTotal]").val(grandTotal.toString());

    I can't find the txtTotal control in your code, etc. So i can't solve the question base on your code, but i made a demo for you as a reference.

    When you delete data from GVbal, you should re-bind data to GVbalS.

    <script>
            $(function () {
                var grandTotal = 0;           
                $("[id*=SBWeight]").each(function () {
                    grandTotal = grandTotal + parseFloat($(this).html());
                });
                $("[id*=lblTotal1]").text(grandTotal.toString());
    
                var grandTotal1 = 0;
                $("[id*=SQTY]").each(function () {
                    grandTotal1 = grandTotal1 + parseFloat($(this).html());
                });
                $("[id*=lblTotal2]").text(grandTotal1.toString());
            });
        </script>
    
    <asp:GridView ID="GVbal" runat="server" AutoGenerateColumns="false" OnRowDeleting="GVbal_RowDeleting">
                <Columns>
                    <asp:TemplateField HeaderText="Sr No">
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Bale_No">
                        <ItemTemplate>
                            <asp:Label ID="BID" runat="server" Text='<%#Bind("BID")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Description">
                        <ItemTemplate>
                            <asp:Label ID="Descriptionitem" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="QTY">
                        <ItemTemplate>
                            <asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Weight">
                        <ItemTemplate>
                            <asp:Label ID="BWeight" runat="server" Text='<%#Bind("BWeight")%>'></asp:Label>
                        </ItemTemplate>              
                    </asp:TemplateField>
    
                    <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
                </Columns>
            </asp:GridView><br />
    
            <asp:GridView ID="GVbalS" runat="server" AutoGenerateColumns="false" ShowFooter="True">
                <Columns>
                    <asp:TemplateField HeaderText="Sr No">
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                    </asp:TemplateField>          
    
                    <asp:TemplateField HeaderText="Description">
                        <ItemTemplate>
                            <asp:Label ID="SDescriptionitem" runat="server" Text='<%#Bind("SDescriptionitem")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="QTY">
                        <ItemTemplate>
                            <asp:Label ID="SQTY" runat="server" Text='<%#Bind("SQTY")%>'></asp:Label>
                        </ItemTemplate>
                         <FooterTemplate><asp:Label ID="lblTotal2" runat="server"></asp:Label></FooterTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Weight">
                        <ItemTemplate>
                            <asp:Label ID="SBWeight" runat="server" Text='<%#Bind("SBWeight")%>'></asp:Label>
                        </ItemTemplate>
                         <FooterTemplate><asp:Label ID="lblTotal1" runat="server"></asp:Label></FooterTemplate>
                     
                    </asp:TemplateField>
    
    
                </Columns>
            </asp:GridView>
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindG2();           
                }
            }
            public void bindG2()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[4] { new DataColumn("BID"), new DataColumn("Descriptionitem"), new DataColumn("QTY"), new DataColumn("BWeight") });
                dt.Rows.Add(100030, "Cut Wool Sweaters 60-100%", 1, 100);
                dt.Rows.Add(100031, "White Cotton Shirts", 1, 500);
                dt.Rows.Add(100030, "Cut Wool Sweaters 60-100%", 1, 100);
                dt.Rows.Add(100035, "Men Cotton Shirts", 1, 100);
                DataTable dt1 = new DataTable();
                dt1.Columns.AddRange(new DataColumn[4] { new DataColumn("SBID"), new DataColumn("SDescriptionitem"), new DataColumn("SQTY"), new DataColumn("SBWeight") });
                foreach (DataRow item in dt.Rows)
                {
                    bool hasRow = false;
                    foreach (DataRow item1 in dt1.Rows)
                    {
                        if (item1["SBID"].ToString() == item["BID"].ToString())
                        {
                            item1["SDescriptionitem"] = item["Descriptionitem"];
                            item1["SQTY"] = Convert.ToInt32(item1["SQTY"]) + Convert.ToInt32(item["QTY"]);
                            item1["SBWeight"] = Convert.ToInt32(item1["SBWeight"]) + Convert.ToInt32(item["BWeight"]);
                            hasRow = true;
                            break;
                        }
                    }
                    if (!hasRow)
                    {
                        dt1.Rows.Add(item["BID"], item["Descriptionitem"], item["QTY"], item["BWeight"]);
    
                    }
                }
                GVbal.DataSource = dt;
                GVbal.DataBind();
                ViewState["dt"] = dt;
                GVbalS.DataSource = dt1;
                GVbalS.DataBind();
            }
    
            protected void GVbal_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                int index = Convert.ToInt32(e.RowIndex);
                DataTable dt = (DataTable)ViewState["dt"];
                dt.Rows[index].Delete();
                ViewState["dt"] = dt;
                GVbal.DataSource = dt;
                GVbal.DataBind();
    
                DataTable dt1 = new DataTable();
                dt1.Columns.AddRange(new DataColumn[4] { new DataColumn("SBID"), new DataColumn("SDescriptionitem"), new DataColumn("SQTY"), new DataColumn("SBWeight") });
                foreach (DataRow item in dt.Rows)
                {
                    bool hasRow = false;
                    foreach (DataRow item1 in dt1.Rows)
                    {
                        if (item1["SBID"].ToString() == item["BID"].ToString())
                        {
                            item1["SDescriptionitem"] = item["Descriptionitem"];
                            item1["SQTY"] = Convert.ToInt32(item1["SQTY"]) + Convert.ToInt32(item["QTY"]);
                            item1["SBWeight"] = Convert.ToInt32(item1["SBWeight"]) + Convert.ToInt32(item["BWeight"]);
                            hasRow = true;
                            break;
                        }
                    }
                    if (!hasRow)
                    {
                        dt1.Rows.Add(item["BID"], item["Descriptionitem"], item["QTY"], item["BWeight"]);
    
                    }
                }
                GVbalS.DataSource = dt1;
                GVbalS.DataBind();
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 13, 2019 8:36 AM