Transfer Selected Rows from one GridView to Another with Total using C# and VB.Net in ASP.Net

Last Reply 4 months ago By dharmendr

Posted 4 months ago

How to show total of selected item of gidview in to another gridview

I just want total value of selected row of one gridview in another gridview to show label in footer showing total value of first gridview.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=chk]').on('change', function () {
            var value = 0;
            $('[id*=chk]:checked').each(function () {
                var row = $(this).closest('tr');
                value = value + parseInt(row.find('[id*=Total]').html());
            });
            $("[id*=gvData] [id*=lblGrandTotal]").html(value);
            $("[id*=gvSelected] [id*=lblGrandTotal]").html(value);
           
        });
    });   
</script>
  

 

         protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[9] { new DataColumn("Name"), new DataColumn("Quantity"), new DataColumn("Contact"), new DataColumn("Email"), new DataColumn("State"), new DataColumn("District"), new DataColumn("Address"), new DataColumn("PQuantity"), new DataColumn("Total") });
                foreach (GridViewRow row in gvData.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox chkRow = (row.Cells[0].FindControl("chk") as CheckBox);
                        if (chkRow.Checked)
                        {
                            string Name = (row.Cells[1].FindControl("Name") as Label).Text;
                            string Quantity = (row.Cells[2].FindControl("Quantity") as Label).Text;
                            string Contact = (row.Cells[3].FindControl("Contact") as Label).Text;
                            string Email = (row.Cells[4].FindControl("Email") as Label).Text;
                            string State = (row.Cells[5].FindControl("State") as Label).Text;
                            string District = (row.Cells[6].FindControl("District") as Label).Text;
                            string Address = (row.Cells[7].FindControl("Address") as Label).Text;
                            string PQuantity = (row.Cells[8].FindControl("PQuantity") as Label).Text;
                            string Total = (row.Cells[9].FindControl("Total") as Label).Text;
                           // lblGrandTotal.Text = Total.ToString(); 

                            dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total);
                        }
                    }
                }
                gvSelected.DataSource = dt;
                gvSelected.DataBind();
            }
            catch(Exception ex)
            {
                Response.Write(ex);
            }
        }

 

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" ShowFooter="true"  Width="100%" CssClass="mydatagrid" BorderColor="SteelBlue" HeaderStyle-BackColor="LightSeaGreen" Font-Size="25px" HorizontalAlign="Center" GridLines="None"  OnRowDataBound="gvData_RowDataBound" >
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="Email">
            <ItemTemplate>
                <asp:Label ID="Email" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="State">
            <ItemTemplate>
                <asp:Label ID="State" runat="server" Text='<%# Eval("State") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="District">
            <ItemTemplate>
                <asp:Label ID="District" runat="server" Text='<%# Eval("District") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="Address">
            <ItemTemplate>
                <asp:Label ID="Address" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="Contact">
            <ItemTemplate>
                <asp:Label ID="Contact" runat="server" Text='<%# Eval("Contact") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="Contact">
            <ItemTemplate>
                <asp:Label ID="PQuantity" runat="server" Text='<%# Eval("PQuantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
           <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>           
                    </ItemTemplate>
        </asp:TemplateField>         
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                 <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>       
                </ItemTemplate>
            <FooterTemplate>
                Total :
                <asp:Label ID="lblGrandTotal" runat="server"  />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
<hr /><u>Selected Rows</u><br />
<asp:GridView ID="gvSelected" runat="server" HeaderStyle-BackColor="#3AC0F2" ShowFooter="true" HeaderStyle-ForeColor="White"
    AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
          <asp:BoundField DataField="State" HeaderText="Country" ItemStyle-Width="150" />
          <asp:BoundField DataField="District" HeaderText="Country" ItemStyle-Width="150" />
          <asp:BoundField DataField="Address" HeaderText="Country" ItemStyle-Width="150" />
          <asp:BoundField DataField="Email" HeaderText="Country" ItemStyle-Width="150" />
          <asp:BoundField DataField="Contact" HeaderText="Country" ItemStyle-Width="150" />
          <asp:BoundField DataField="PQuantity" HeaderText="Country" ItemStyle-Width="150" />
          <asp:BoundField DataField="Total" HeaderText="Country" ItemStyle-Width="150" />
          <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                 <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>       
                </ItemTemplate>
            <FooterTemplate>
                Total :
                <asp:Label ID="lblGrandTotal" runat="server"  />
            </FooterTemplate>
          </asp:TemplateField>     
    </Columns> 
</asp:GridView>

 

Posted 4 months ago

Hi VinitaAkha,

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

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('[id*=chk]').on('change', function () {
                var value = 0;
                $('[id*=chk]:checked').each(function () {
                    var row = $(this).closest('tr');
                    value = value + parseInt(row.find('[id*=Total]').html());
                });
                $("[id*=gvData] [id*=lblGrandTotal]").html(value);
                $("[id*=gvSelected] [id*=lblGrandTotal]").html(value);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" ShowFooter="true"
            Width="100%">
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="Email" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:Label ID="State" runat="server" Text='<%# Eval("State") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="District">
                    <ItemTemplate>
                        <asp:Label ID="District" runat="server" Text='<%# Eval("District") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="Address" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Contact">
                    <ItemTemplate>
                        <asp:Label ID="Contact" runat="server" Text='<%# Eval("Contact") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PQuantity">
                    <ItemTemplate>
                        <asp:Label ID="PQuantity" runat="server" Text='<%# Eval("PQuantity") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        Total :
                        <asp:Label ID="lblGrandTotal" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView><br />
        <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
        <hr />
        <u>Selected Rows</u>
        <br /><br />
        <asp:GridView ID="gvSelected" runat="server" Width="100%" ShowFooter="true" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="State" HeaderText="State" />
                <asp:BoundField DataField="District" HeaderText="District" />
                <asp:BoundField DataField="Address" HeaderText="Address" />
                <asp:BoundField DataField="Email" HeaderText="Email" />
                <asp:BoundField DataField="Contact" HeaderText="Contact" />
                <asp:BoundField DataField="PQuantity" HeaderText="PQuantity" />
                <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        Total :
                        <asp:Label ID="lblGrandTotal" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[10] { 
                                new DataColumn("Role", typeof(string)),
                                new DataColumn("Email", typeof(string)),
                                new DataColumn("Contact",typeof(string)),
                                new DataColumn("State", typeof(string)),
                                new DataColumn("District", typeof(string)),
                                new DataColumn("Address",typeof(string)),
                                new DataColumn("Name", typeof(string)),
                                new DataColumn("Quantity", typeof(int)),
                                new DataColumn("PQuantity",typeof(int)),
                                new DataColumn("Total",typeof(int))});
        dt.Rows.Add("Role 1", "Email 1", "Contact 1", "State 1", "District 1", "Address 1", "Name 1", 10, 5, 50);
        dt.Rows.Add("Role 2", "Email 2", "Contact 2", "State 2", "District 2", "Address 2", "Name 2", 20, 5, 100);
        dt.Rows.Add("Role 3", "Email 3", "Contact 3", "State 3", "District 3", "Address 3", "Name 3", 15, 5, 75);
        dt.Rows.Add("Role 4", "Email 4", "Contact 4", "State 4", "District 4", "Address 4", "Name 4", 12, 5, 60);
        gvData.DataSource = dt;
        gvData.DataBind();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    decimal total = 0;
    try
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[9] { 
                new DataColumn("Name"), 
                new DataColumn("Quantity"), 
                new DataColumn("Contact"), 
                new DataColumn("Email"), 
                new DataColumn("State"), 
                new DataColumn("District"), 
                new DataColumn("Address"), 
                new DataColumn("PQuantity"), 
                new DataColumn("Total") });
        foreach (GridViewRow row in gvData.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chk") as CheckBox);
                if (chkRow.Checked)
                {
                    string Name = (row.Cells[1].FindControl("Name") as Label).Text;
                    string Quantity = (row.Cells[2].FindControl("Quantity") as Label).Text;
                    string Contact = (row.Cells[3].FindControl("Contact") as Label).Text;
                    string Email = (row.Cells[4].FindControl("Email") as Label).Text;
                    string State = (row.Cells[5].FindControl("State") as Label).Text;
                    string District = (row.Cells[6].FindControl("District") as Label).Text;
                    string Address = (row.Cells[7].FindControl("Address") as Label).Text;
                    string PQuantity = (row.Cells[8].FindControl("PQuantity") as Label).Text;
                    string Total = (row.Cells[9].FindControl("Total") as Label).Text;
                    total += Convert.ToDecimal(Total);
                    dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total);
                }
            }
        }

        gvSelected.DataSource = dt;
        gvSelected.DataBind();

        (gvSelected.FooterRow.FindControl("lblGrandTotal") as Label).Text = total.ToString();
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(9) {New DataColumn("Role", GetType(String)), New DataColumn("Email", GetType(String)), New DataColumn("Contact", GetType(String)), New DataColumn("State", GetType(String)), New DataColumn("District", GetType(String)), New DataColumn("Address", GetType(String)), New DataColumn("Name", GetType(String)), New DataColumn("Quantity", GetType(Integer)), New DataColumn("PQuantity", GetType(Integer)), New DataColumn("Total", GetType(Integer))})
        dt.Rows.Add("Role 1", "Email 1", "Contact 1", "State 1", "District 1", "Address 1", "Name 1", 10, 5, 50)
        dt.Rows.Add("Role 2", "Email 2", "Contact 2", "State 2", "District 2", "Address 2", "Name 2", 20, 5, 100)
        dt.Rows.Add("Role 3", "Email 3", "Contact 3", "State 3", "District 3", "Address 3", "Name 3", 15, 5, 75)
        dt.Rows.Add("Role 4", "Email 4", "Contact 4", "State 4", "District 4", "Address 4", "Name 4", 12, 5, 60)
        gvData.DataSource = dt
        gvData.DataBind()
    End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim total As Decimal = 0

    Try
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(8) {New DataColumn("Name"), New DataColumn("Quantity"), New DataColumn("Contact"), New DataColumn("Email"), New DataColumn("State"), New DataColumn("District"), New DataColumn("Address"), New DataColumn("PQuantity"), New DataColumn("Total")})

        For Each row As GridViewRow In gvData.Rows

            If row.RowType = DataControlRowType.DataRow Then
                Dim chkRow As CheckBox = (TryCast(row.Cells(0).FindControl("chk"), CheckBox))

                If chkRow.Checked Then
                    Dim Name As String = (TryCast(row.Cells(1).FindControl("Name"), Label)).Text
                    Dim Quantity As String = (TryCast(row.Cells(2).FindControl("Quantity"), Label)).Text
                    Dim Contact As String = (TryCast(row.Cells(3).FindControl("Contact"), Label)).Text
                    Dim Email As String = (TryCast(row.Cells(4).FindControl("Email"), Label)).Text
                    Dim State As String = (TryCast(row.Cells(5).FindControl("State"), Label)).Text
                    Dim District As String = (TryCast(row.Cells(6).FindControl("District"), Label)).Text
                    Dim Address As String = (TryCast(row.Cells(7).FindControl("Address"), Label)).Text
                    Dim PQuantity As String = (TryCast(row.Cells(8).FindControl("PQuantity"), Label)).Text
                    Dim Total As String = (TryCast(row.Cells(9).FindControl("Total"), Label)).Text
                    total += Convert.ToDecimal(Total)
                    dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total)
                End If
            End If
        Next

        gvSelected.DataSource = dt
        gvSelected.DataBind()
        (TryCast(gvSelected.FooterRow.FindControl("lblGrandTotal"), Label)).Text = total.ToString()
    Catch ex As Exception
        Response.Write(ex)
    End Try
End Sub

Screenshot