C # easy way to copy or clone DataRow?

I am looking for an easy way to make a DataRow clone. This is similar to capturing this line and saving it. The values โ€‹โ€‹of the original Row can then be changed, but we have another saved copy that does not change. Is this the right way to do this?

DataRow Source, Destination; //Assume we create some columns and fill them with values Destination.ItemArray = Source.ItemArray; 

Will this only point to the Snapshot ItemArray link to what the source in the source points to, or is it really making a separate copy? Should I do this instead?

  Destination.ItemArray = Source.ItemArray.Clone(); 

EDIT: I don't think the second piece of code really compiles.

+91
c # datatable datarow
Aug 19 2018-12-12T00:
source share
2 answers

You can use the ImportRow method to copy a row from a DataTable to a DataTable using the same scheme:

 var row = SourceTable.Rows[RowNum]; DestinationTable.ImportRow(row); 

Update:

With your new Edit, I find:

 var desRow = dataTable.NewRow(); var sourceRow = dataTable.Rows[rowNum]; desRow.ItemArray = sourceRow.ItemArray.Clone() as object[]; 

will work

+147
Aug 19 2018-12-12T00: 00Z
source share

But to make sure your new row is available in the new table, you need to close the table:

 DataTable Destination = new DataTable(Source.TableName); Destination = Source.Clone(); DataRow sourceRow = Source.Rows[0]; Destination.ImportRow(sourceRow); 
-one
Apr 04 '15 at 19:03
source share



All Articles