Inserting Values ​​into Datatables

I have a DataTable filled with values ​​from a database. I have a different datatype that I am creating that has the same column names from the first one, forget some extra data I need to generate the CSV along the line. What I would like to do is take data from the first table and use it to populate the second table and then I can populate other data.

I know I can do this very mechanically, looping, creating new rows and then filling the cell for the cell from the first table.

I hope someone knows something small.

Jim

+1


a source to share


2 answers


I think this is what you are looking for DataTable.Merge



+3


a source


You can use the Merge method on datatable. The only thing you need to know is that there must be a primary key for both DataTables. Probably the one that comes from the DB already does, but then the one that you populate in memory most likely doesn't.

You can easily set the primary key like this:



table.PrimaryKey = new DataColumn[] { table.Columns[0]}; //or whatever column you want.

      

+2


a source







All Articles