Transfer ASP.Net GridView Selected Rows from one page to another using CheckBox in C# and VB.Net

Last Reply 11 days ago By pandeyism

Posted 11 days ago

I want my one session login Datatable can fetch in other session page load time without button click.

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False"    HeaderStyle-Height="100px"  ShowFooter="True"  Width="100%"   Font-Size="25px" HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" GridLines="None" >
    <AlternatingRowStyle BackColor="White" />
    <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>           
    </Columns>
</asp:GridView>
<center>
    <asp:Button ID="Button1" runat="server" Text="Add" ForeColor="Red" OnClick="Button1_Click" Font-Size="20px" CssClass="bg-primary" Width="100px" Height="50px" />
</center><hr />
<center>
<u style="font-size:30px">Selected Rows</u></center><br />
<asp:GridView ID="gvSelected" runat="server"  Width="100%" BorderColor="White" Font-Size="25px" HeaderStyle-BackColor="#006699" HorizontalAlign="Center"  ShowFooter="true" HeaderStyle-ForeColor="White" GridLines="None"
    AutoGenerateColumns="false">
    <Columns>       
         <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Label ID="lblGrandTotal" runat="server"  />

 

second page......
<asp:GridView ID="gvSelected" runat="server"></asp:GridView>

 

DataSet ds2 = new DataSet();
ds2 = (DataSet)Session["ds"];
gvSelected.DataSource = ds2;
gvSelected.DataBind();

 

 

Posted 11 days ago

Hi VinitaAkha,

Refer below sample.

HTML

CS.aspx

<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>
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="Transfer" />
            </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>

Default.aspx

<u>Selected Rows</u>
<br />
<br />
<asp:GridView ID="gvSelected" runat="server" 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>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

CS.aspx.cs

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 Transfer(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);
                }
            }
        }
        Session["dt"] = dt;
        Session["Tatal"] = total;
        Response.Redirect("Default.aspx");
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = Session["dt"] as DataTable;
        gvSelected.DataSource = dt;
        gvSelected.DataBind();
        (gvSelected.FooterRow.FindControl("lblGrandTotal") as Label).Text = Session["Tatal"].ToString();
    }
}

VB.Net

VB.aspx.vb

Class SurroundingClass
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
        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 Transfer(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
            Session("dt") = dt
            Session("Tatal") = total
            Response.Redirect("Default.aspx")
        Catch ex As Exception
            Response.Write(ex)
        End Try
    End Sub
End Class

DefaultVB.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = TryCast(Session("dt"), DataTable)
        gvSelected.DataSource = dt
        gvSelected.DataBind()
        (TryCast(gvSelected.FooterRow.FindControl("lblGrandTotal"), Label)).Text = Session("Tatal").ToString()
    End If
End Sub

Screenshot