Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one problem while transfer grdiview checkbox selecteed rows from one web form to another webform

this is my code but it will transfer only one row i want to transfer all selected rows

WebForm1--------------------------
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SelectedGrid.aspx.cs" Inherits="SelectedGrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"  runat="server">
    <title>Add Delete Selected rows from one Gridview to another</title>
    <script type = "text/javascript">
<!--
        function Check_Click(objRef) {
            //Get the Row based on checkbox
            var row = objRef.parentNode.parentNode;

            //Get the reference of GridView
            var GridView = row.parentNode;

            //Get all input elements in Gridview
            var inputList = GridView.getElementsByTagName("input");

            for (var i = 0; i < inputList.length; i++) {
                //The First element is the Header Checkbox
                var headerCheckBox = inputList[0];

                //Based on all or none checkboxes
                //are checked check/uncheck Header Checkbox
                var checked = true;
                if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
                    if (!inputList[i].checked) {
                        checked = false;
                        break;
                    }
                }
            }
            headerCheckBox.checked = checked;

        }
        function checkAll(objRef) {
            var GridView = objRef.parentNode.parentNode.parentNode;
            var inputList = GridView.getElementsByTagName("input");
            for (var i = 0; i < inputList.length; i++) {
                var row = inputList[i].parentNode.parentNode;
                if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                    if (objRef.checked) {
                        inputList[i].checked = true;
                    }
                    else {
                        if (row.rowIndex % 2 == 0) {
                            row.style.backgroundColor = "#C2D69B";
                        }
                        else {
                            row.style.backgroundColor = "white";
                        }
                        inputList[i].checked = false;
                    }
                }
            }
        }
//-->
</script>
</head>
<body>
    <form id="form1"  runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager2" runat="server">
            
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <contenttemplate>
                    <asp:GridView ID="gvAll" runat="server" AllowPaging="true" 
                        AlternatingRowStyle-BackColor="#C2D69B" AutoGenerateColumns="false" 
                        Font-Names="Arial" Font-Size="11pt" HeaderStyle-BackColor="green" 
                        OnPageIndexChanging="OnPaging" PageSize="10">
                        <columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" 
                                         onclick="checkAll(this);" />
                                </HeaderTemplate>
                                <itemtemplate>
                                    <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" 
                                         onclick="Check_Click(this)" />
                                </itemtemplate>
                            
                            <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" 
                                HtmlEncode="false" />
                            <asp:BoundField DataField="ContactName" HeaderText="Contact Name" 
                                HtmlEncode="false" />
                            <asp:BoundField DataField="City" HeaderText="City" HtmlEncode="false" />
                        </columns>
                        <alternatingrowstyle backcolor="#C2D69B" />
                    
                </contenttemplate>
            
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                Text="Button" />
            <br />

        </div>
    </form>
</body>
</html>

C#Code-------------------------------------For WebForm1
C#
using System;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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.Collections;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPrimaryGrid();
          
          
        }
    }

    private void BindPrimaryGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        string query = "select CustomerID, ContactName, City from customers";
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        gvAll.DataSource = dt;
        gvAll.DataBind();  
    }

protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GetData();
    gvAll.PageIndex = e.NewPageIndex;
    BindPrimaryGrid();
    SetData(); 
}

private void GetData()
{
    DataTable dt;
    if (Session["SelectedRecords"] != null)
        
        dt = (DataTable)ViewState["SelectedRecords"];
    else
        dt = CreateDataTable();
    CheckBox chkAll = (CheckBox)gvAll.HeaderRow
                        .Cells[0].FindControl("chkAll");
    for (int i = 0; i < gvAll.Rows.Count; i++)
    {
        if (chkAll.Checked)
        {
            dt = AddRow(gvAll.Rows[i], dt);
        }
        else
        {
            CheckBox chk = (CheckBox)gvAll.Rows[i]
                            .Cells[0].FindControl("chk");
            if (chk.Checked)
            {
                dt = AddRow(gvAll.Rows[i], dt);
            }
            else
            {
                dt = RemoveRow(gvAll.Rows[i], dt);
            }
        }
    }
    Session["SelectedRecords"] = dt;
}

    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gvAll.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        if (ViewState["SelectedRecords"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRecords"];
            for (int i = 0; i < gvAll.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvAll.Rows[i].Cells[0].FindControl("chk");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("CustomerID = '" + gvAll.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0; 
                    if (!chk.Checked)
                    {
                        chkAll.Checked = false;
                    }
                }
            }
        }
    }

private DataTable CreateDataTable()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("CustomerID");
    dt.Columns.Add("ContactName");
    dt.Columns.Add("City");
    dt.AcceptChanges();
    return dt;
}

private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
    DataRow[] dr = dt.Select("CustomerID = '" + gvRow.Cells[1].Text + "'");
    if (dr.Length <= 0)
    {
        dt.Rows.Add();
        dt.Rows[dt.Rows.Count - 1]["CustomerID"] = gvRow.Cells[1].Text;
        dt.Rows[dt.Rows.Count - 1]["ContactName"] = gvRow.Cells[2].Text;
        dt.Rows[dt.Rows.Count - 1]["City"] = gvRow.Cells[3].Text;
        dt.AcceptChanges();
    }
    return dt;
}

private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
    DataRow[] dr = dt.Select("CustomerID = '" + gvRow.Cells[1].Text + "'");
    if (dr.Length > 0)
    {
        dt.Rows.Remove(dr[0]);
        dt.AcceptChanges();
    }
    return dt;
}

protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
    //GetData();
    //SetData();
    
}


protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("Retrieve.aspx");
}
}

WEBForm2-----------------------------------------------------
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RetrievedGrid.aspx.cs" Inherits="RetrievedGrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
      <asp:GridView ID="gvSelected" runat="server" 
AutoGenerateColumns = "false" Font-Names = "Arial" 
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
HeaderStyle-BackColor = "green" EmptyDataText = "No Records Selected"  >
<columns>
    <asp:BoundField DataField = "CustomerID" HeaderText = "Customer ID" />
    <asp:BoundField DataField = "ContactName" HeaderText = "Contact Name" />
    <asp:BoundField DataField = "City" HeaderText = "City" />
 </columns>

    </div>
    </form>
</body>
</html>

C# Code --------------------------------For webform2
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class RetrievedGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindSecondaryGrid();
    }
    

       private void BindSecondaryGrid()
    {
        DataTable dt = (DataTable)Session["SelectedRecords"];
          gvSelected.DataSource = dt;
         gvSelected.DataBind(); 
    }
    
}
Posted
Updated 27-Oct-11 2:33am
v3
Comments
Muralikrishna8811 27-Oct-11 8:27am    
where you call getdata() to store your selected data in session
yerrojumeher 27-Oct-11 8:31am    
i didnt get you murali. can u explain it clearly so that.we can understand
DGKumar 27-Oct-11 8:34am    
i have updated the Question pls check.
Muralikrishna8811 27-Oct-11 9:11am    
Try this

protected void Button1_Click(object sender, EventArgs e)
{
GetData();
Response.Redirect("Retrieve.aspx");
}
Muralikrishna8811 27-Oct-11 9:12am    
And check first your selected rows are shown in server side or not

1 solution

In your GetData() method on Webform 1 you are checking Session object for null and picking up data from ViewState object while populating datatable. See the bold highlighting in the code below--

SQL
if (Session["SelectedRecords"] != null)

        dt = (DataTable)ViewState["SelectedRecords"];
    else
        dt = CreateDataTable();


I doubt the working of your code ???
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900