Display Summary of GridView column in another GridView using C# in ASP.Net

Last Reply one month ago By arunkurmi

Posted one month ago

i am inserting data into gridview

now i want summary of all item in another gridview on same page

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

here is image for your consideration

https://i.ibb.co/LrYcL12/g.jpg

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

html code 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BigbaleDispatch.aspx.cs"
    Inherits="WebApplication1.BigbaleDispatch" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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>
</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-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">
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            </td>
            <td>
                &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>
        </tr>
    </table>
    </form>
</body>
</html>

insert item into gridview code c#

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 = "";
}

 

You are viewing reply posted by: arunkurmi one month ago.
Posted one month ago Modified on one month ago

Hi akhter,

Check this example. Now please take its reference and correct your code.

HTML

<div class="auto-style1">
    <h1>
        <strong>BigBale Dispatch&nbsp;&nbsp;&nbsp;&nbsp; </strong>
    </h1>
</div>
<table class="auto-style2">
    <tr>
        <td class="auto-style7">
            Bale No Input :
        </td>
        <td class="auto-style8">
            <asp:TextBox ID="txtsearcbn" runat="server" Height="24px" Width="127px"></asp:TextBox>
            <asp:DropDownList ID="DDLitem" runat="server">
                <asp:ListItem Value="1">Cut wool Sweaters 60-100%</asp:ListItem>
                <asp:ListItem Value="2">White Cotton Shirts</asp:ListItem>
                <asp:ListItem Value="3">Mens Cotton Shirts</asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="txtqty" runat="server" Height="16px" Width="18px"></asp:TextBox>
            <asp:TextBox ID="txtwt" runat="server" 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="btnadd_Click" />
        </td>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td class="auto-style5">
            &nbsp;
        </td>
        <td class="auto-style6">
            <asp:GridView ID="GVbal" runat="server" OnRowDeleting="GVbal_RowDeleting" AutoGenerateColumns="false"
                DataKeyNames="Index">
                <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="SBWeight" runat="server" Text='<%#Bind("BWeight")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
                </Columns>
            </asp:GridView>
        </td>
        <td>
            <asp:Label ID="lblSummary" Text="Sumary GridView" runat="server" /><br />
            <asp:GridView runat="server" ID="gvSummary" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Sr No.">
                        <ItemTemplate>
                            <%# Container.DataItemIndex +1 %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description">
                        <ItemTemplate>
                            <asp:Label ID="lblDescription" runat="server" Text='<%#Bind("Description")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="QTY">
                        <ItemTemplate>
                            <asp:Label ID="lblQTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Weight">
                        <ItemTemplate>
                            <asp:Label ID="lblBWeight" runat="server" Text='<%#Bind("Weight")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </td>
    </tr>
</table>

Namespaces

C#

using System.Data;
using System.Linq;

 

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Index", typeof(Int32)));
        dt.Columns.Add(new DataColumn("BID", typeof(Int32)));
        dt.Columns.Add(new DataColumn("Codeitem", typeof(string)));
        dt.Columns.Add(new DataColumn("Descriptionitem", typeof(string)));
        dt.Columns.Add(new DataColumn("QTY", typeof(Int32)));
        dt.Columns.Add(new DataColumn("BWeight", typeof(Int32)));

        ViewState["dt"] = dt;
    }
}

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("BID") as Label).Text; // If TemplateField use FindControl.
                if (bId == txtsearcbn.Text.Trim())
                {
                    ifExist = true;
                    break;
                }
            }
        if (!ifExist)
        {
            dr2["Index"] = dtgridview.Rows.Count + 1;
            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();
            BindSummaryGrid(dtgridview);
        }
        else
        {
            // this.lbgvck.Visible = true;
            Response.Write("<script language='javascript'>alert('Bale Already Added');</script>");
        }
        clear();
    }
}

protected void BindSummaryGrid(DataTable dt)
{
    DataTable dtSummary = new DataTable();
    dtSummary.Columns.Add(new DataColumn("Description", typeof(string)));
    dtSummary.Columns.Add(new DataColumn("QTY", typeof(int)));
    dtSummary.Columns.Add(new DataColumn("Weight", typeof(int)));
    var result = (from row in dt.AsEnumerable()
                    group row by new { Description = row["Descriptionitem"] } into g
                    select new
                    {
                        g.Key.Description,
                        QTY = g.Sum(x => Convert.ToDecimal(x["QTY"])),
                        Weight = g.Sum(x => Convert.ToDecimal(x["BWeight"]))
                    });
    foreach (var item in result)
    {
        dtSummary.Rows.Add(item.Description, item.QTY, item.Weight);
    }
    gvSummary.DataSource = dtSummary;
    gvSummary.DataBind();
}

protected void GVbal_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string index = GVbal.DataKeys[e.RowIndex].Values[0].ToString();
    DataTable dt = ViewState["dt"] as DataTable;
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (dt.Rows[i]["Index"].ToString() == index)
        {
            dt.Rows.Remove(dt.Rows[i]);
            ViewState["dt"] = dt;
            GVbal.DataSource = dt;
            GVbal.DataBind();
            BindSummaryGrid(dt);
            break;
        }
    }
}

private void clear()
{
    txtsearcbn.Text = "";
}

Screenshots