A Quick Way to Copy a DataRow in C#

This is fairly trivial. I just needed to remind myself of it. This can be found here.

1
2
3
4
5
6
7
8
DataTable dtDest = new DataTable();
dtDest = dsActivity.Tables[0].Clone();
 
foreach(DataRow dr in dsSrc.Tables[0].Rows){
    DataRow newRow = dtDest .NewRow();
    newRow.ItemArray = dr.ItemArray;
    dtDest.Rows.Add(newRow);
}
  1. If you're copying all rows, why not just use:
    DataTable dtDest = dsSrc.Tables[0].Copy();

  2. In all honesty I'm not sure, but that seems that it would work just as well. Thanks for pointing that out.

  3. This is exactly what I'm looking for. Thanks.

  4. And it's working fine.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">