none
How to copy datarow from one datatable to another ?

    Question

  • can anyone tell me please how to copy one datarow from one datatable to another.I want to extract some specific rows from one datatable and put into antoher.it doesn't show data in my datagridview . Here is my code:

     

    SqlCommand cmd = new SqlCommand("Test", new Setting().Getconn());
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Add("@ghid", Convert.ToInt32(textBox2.Text));
          SqlDataReader rd = cmd.ExecuteReader();
          DataTable dt = new DataTable();
          dt.Load(rd);
          //-------------------
          SqlCommand cmd2 = new SqlCommand("Test2", new Setting().Getconn());
          cmd2.CommandType = CommandType.StoredProcedure;
          cmd2.Parameters.Add("@ghid", Convert.ToInt32(textBox1.Text));
          SqlDataReader rd2 = cmd2.ExecuteReader();
          DataTable dt2 = new DataTable();
          dt2.Load(rd2);
          
          //-----------------
          DataTable dtotal = new DataTable();
          //-----------------
          foreach (DataRow dr in dt.Rows)
          {
            foreach (DataRow dr2 in dt2.Rows)
            {
              if (dr["Gid"].ToString() == dr2["FGid"].ToString())
              {
                dtotal.ImportRow(dr2);
              }
              continue;
            }            
           continue;   
          }
          dtotal=dt2.Clone();
          dataGridView1.DataSource = dtotal;
            
        }

    Friday, June 18, 2010 2:33 PM

Answers

  • You need to change code as below. Hope it will work for you.

    SqlCommand cmd = new SqlCommand("Test", new Setting().Getconn());
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add("@ghid", Convert.ToInt32(textBox2.Text));
       SqlDataReader rd = cmd.ExecuteReader();
       DataTable dt = new DataTable();
       dt.Load(rd);
       //-------------------
       SqlCommand cmd2 = new SqlCommand("Test2", new Setting().Getconn());
       cmd2.CommandType = CommandType.StoredProcedure;
       cmd2.Parameters.Add("@ghid", Convert.ToInt32(textBox1.Text));
       SqlDataReader rd2 = cmd2.ExecuteReader();
       DataTable dt2 = new DataTable();
       dt2.Load(rd2);
       
       //-----------------
       DataTable dtotal = new DataTable();
       dtotal=dt2.Clone();
       //-----------------
       foreach (DataRow dr in dt.Rows)
       {
        foreach (DataRow dr2 in dt2.Rows)
        {
         if (dr["Gid"].ToString() == dr2["FGid"].ToString())
         {
          dtotal.ImportRow(dr2);
         }
         continue;
        }      
        continue;  
       }
     
       dataGridView1.DataSource = dtotal;
        
      }
    

    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Babak_bsn Saturday, June 19, 2010 7:40 AM
    Friday, June 18, 2010 2:51 PM