Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Button2_Click(object sender, EventArgs e)
   {
       DataTable dtNew = new DataTable("Employee Detils");
       dtNew.Columns.Add(new DataColumn() { ColumnName = "EmpName", DataType = typeof(String) });
       dtNew.Columns.Add(new DataColumn() { ColumnName = "EmpId", DataType = typeof(String) });



       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
           {
               DataRow dr = dtNew.NewRow();

               dr["EmpName"] = GridView1.Rows[i].FindControl("button2");
               dr["EmpId"] = GridView1.Rows[i].FindControl("button2");

               dtNew.Rows.Add(dr);
               Session["Employee Details"] = dtNew;
               if (dtNew.Rows.Count > 0)
               {
                   string a = "Mr." + dr["EmpName"];
                   string b = "+91" + dr["EmpId"];
                   dr["EmpName"] = a;

                   dr["EmpId"] = b;

                   dtNew.Rows.Add(a, b);


                   dtNew = Session["Employee Details"] as DataTable;
                   GridView2.DataSource = dtNew;
                   GridView2.DataBind();

               }
           }
       }
   }


What I have tried:

yes i have tried.................... i have binded gridview1 with datatable. to bind few columns to my gridview2 from gridview1 without using database, i am getting problems....with my code appraoch. Can any one please guide me?
Posted
Updated 7-May-16 0:05am
v2
Comments
Karthik_Mahalingam 7-May-16 4:33am    
what is the problem

1 solution

try this

C#
protected void btnSave_Click(object sender, EventArgs e)
      {
          DataTable dtNew = new DataTable("Employee Detils");
          dtNew.Columns.Add(new DataColumn() { ColumnName = "EmpName", DataType = typeof(String) });
          dtNew.Columns.Add(new DataColumn() { ColumnName = "EmpId", DataType = typeof(String) });


          foreach (GridViewRow row in GridView1.Rows)
              if (row.RowType == DataControlRowType.DataRow)
              {
                  DataRow dr = dtNew.NewRow();
                  string empName = (row.FindControl("YourEmpNameControlId") as TextBox).Text;  // If it is a button replace the Textbox with button or the relevant control
                  string empId = (row.FindControl("YourEmpIdControlId") as TextBox).Text;  // If it is a button replace the Textbox with button or the relevant control
                  dr["EmpName"] = "Mr." + empName;
                  dr["EmpId"] = "+91" + empId;
                  dtNew.Rows.Add(dr);
              }
          GridView2.DataSource = dtNew;
          GridView2.DataBind();

      }
 
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