ASP.NET - transfer value from one gridview to another gridview

Asked By msakt on 05-Jun-12 03:42 AM
DataTable dtLeft = new DataTable("Supplier");

        dtLeft.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
        dtLeft.Columns.Add(new DataColumn("Code"));
        dtLeft.Columns.Add(new DataColumn("Name"));

        dtLeft.Rows.Add(1, "st0001", "S.R. Steel");
        dtLeft.Rows.Add(2, "ir0039", "Shadesh builders");
        dtLeft.Rows.Add(3, "cr0042", "Orchard confec.");
        dtLeft.Rows.Add(4, "er0078", "Windblow");
        dtLeft.Rows.Add(5, "bd0301", "Rahimkarim");
        gvLeft.DataSource = dtLeft;
        gvLeft.DataBind();

if i bind like above..value transfer leftgrid to right grid  but if bind like this

        string s = " select regularmemberid, firstname from regularmemberdetails";
        SqlCommand cmd = new SqlCommand(s, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(dtLeft);
        gvLeft.DataSource = dtLeft;
        gvLeft.DataBind();


does not work.......
Ravi Maurya replied to msakt on 05-Jun-12 03:51 AM
Refer this article

http://www.aspsnippets.com/Articles/Transfer-Selected-Rows-from-one-GridView-to-Another-in-Asp.net.aspx
dipa ahuja replied to msakt on 05-Jun-12 03:56 AM
If you are binding the data from the table then you can just write it as :

Gridview2.DataSource = GridView1.DataSource;

or if you just going to add only selected Rows then :

Take a CheckBox inside the Gridview' ItemTemplate
 
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
 
 
Take a Button to pass data to next Page
 
<asp:Button ID="btnTransfer" runat="server" onclick="btnTransfer_Click"
  Text="Send" />
 
Write this code in the Button click
DataTable dtPass = new DataTable();
 
protected void btnTransfer_Click(object sender, EventArgs e)
{
  dtPass.Columns.Add("Name", typeof(string));
  dtPass.Columns.Add("Gender", typeof(string));
 
  foreach (GridViewRow row in GridView1.Rows)
  {
    if (((CheckBox)row.FindControl("checkBox1")).Checked == true)
    {
      //Get data of ItemTEmplate of table Fields
      string lblname = ((Label)row.FindControl("lblName")).Text;
      string lblgender = ((Label)row.FindControl("lblGender")).Text;
 
      DataRow dtRow = dtPass.NewRow();
      dtRow["Name"] = lblname;
      dtRow["Gender"] = lblgender;
      dtPass.Rows.Add(dtRow);
    }
  }
  
}
 
Now assing this dt :
GridView2.DataSource = dtPass;

GridView2.DataBind();
  
 
 
 

Jitendra Faye replied to msakt on 05-Jun-12 04:37 AM
First check that you are getting any data from database or not.

Out break point in this line-


string s = " select regularmemberid, firstname from regularmemberdetails";
      SqlCommand cmd = new SqlCommand(s, con);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(dtLeft);
      gvLeft.DataSource = dtLeft;
      gvLeft.DataBind();


And check for data in dataset -dtLeft.

if it contains data then data will be display in GridVIew.