Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
transfer selected rows from one gridview to another in next page on btnclick evnet

What I have tried:

transfer selected rows from one gridview to another in next page on btnclick evnet
Posted
Updated 3-Oct-17 22:35pm
Comments
Karthik_Mahalingam 4-Oct-17 1:59am    
post the code what you have tried?
is it a read only rows or contains template columns?
[no name] 4-Oct-17 3:01am    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="310px" style="margin-right: 56px" Width="704px">
<columns>
<asp:templatefield HeaderText="Select">
<itemtemplate>
<asp:checkbox ID="cbSelect"
CssClass="gridCB" runat="server">


<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="events" HeaderText="events" />










protected void Page_Load(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Calender event selection list"];
SPListItemCollection items = list.Items;
GridView1.DataSource = items.GetDataTable();
GridView1.DataBind();
}
design and cs code i have attached , please provide with a code so that when i select those checkbox and click on button i get only selected rows diplayed on seperate grid view on another sharepoint page
Karthik_Mahalingam 4-Oct-17 4:19am    
how does the gridview in other page looks like
[no name] 4-Oct-17 8:59am    
i have done only till this
GKP1992 4-Oct-17 2:29am    
You can try to have a column selected in your data set, set them to true in javascript when selecting. Then access on the next page (or anywhere you want based on the accessibility of your data set).

1 solution

try
Page 1

<form id="form1" runat="server">

       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="310px" Style="margin-right: 56px" Width="704px">
           <Columns>
               <asp:TemplateField HeaderText="Select">
                   <ItemTemplate>
                       <asp:CheckBox ID="cbSelect"
                           CssClass="gridCB" runat="server"></asp:CheckBox>

                   </ItemTemplate>
               </asp:TemplateField>

               <asp:BoundField DataField="Title" HeaderText="Title" />
               <asp:BoundField DataField="Date" HeaderText="Date" />
               <asp:BoundField DataField="events" HeaderText="events" />
           </Columns>
       </asp:GridView>
       <asp:Button ID="btnCopy" runat="server" Text="Copy Rows" OnClick="btnCopy_Click" />

   </form>


public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists["Calender event selection list"];
                SPListItemCollection items = list.Items;
                GridView1.DataSource = items.GetDataTable();
                GridView1.DataBind();
            }
        }

      

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns.Add("Date");
            dt.Columns.Add("events");

            foreach (GridViewRow row in GridView1.Rows)
                if (((CheckBox)row.Cells[0].FindControl("cbSelect")).Checked)
                    dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text, row.Cells[3].Text);

            Session["Data"] = dt;
            Response.Redirect("WebForm2.aspx");
        }
    }


Page 2

<form id="form1" runat="server">
   <div>

       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="310px" Style="margin-right: 56px" Width="704px">
           <Columns>
               <asp:BoundField DataField="Title" HeaderText="Title" />
               <asp:BoundField DataField="Date" HeaderText="Date" />
               <asp:BoundField DataField="events" HeaderText="events" />
           </Columns>
       </asp:GridView>
   </div>
   </form>


 public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) {
                DataTable dt = (DataTable)Session["Data"];
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }
    }
}
 
Share this answer
 
v2
Comments
[no name] 4-Oct-17 8:45am    
thanku but can u tell me where to paste these code as i am developing a visual webpart solution
Karthik_Mahalingam 4-Oct-17 23:35pm    
add in the ascx and cs file
[no name] 5-Oct-17 0:47am    
thanku karthik for immediate solution; but still i am adding ascx and cs file for page 1 and where do i add page 2 solution
[no name] 5-Oct-17 1:19am    
i added in ascx file -- code for page 1 it showing error as session is not in current context and where do i add code for second page
[no name] 5-Oct-17 1:32am    
if i am adding it as such i am getting an error as cannot access gridview1 via nested type Cannot access a non-static member of outer type 'SharePointProject6.VisualWebPart6.VisualWebPart6' via nested type 'SharePointProject6.VisualWebPart6.VisualWebPart6.WebForm1'

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