Saturday, August 23, 2008

.net DataTable's Tricks

Copy Structure from another DataTable

Keyword : Clone
Usage : DataTable2 = DataTable1.Clone
Note : This method will only copies structure , schema & constraint from another DataTable , but not data


Copy Structure + Data from another DataTable

Keyword : Copy
Usage : DataTable2 = DataTable1.Copy
Note : Copies both structure and data from another DataTable


Combining two DataTables

Keyword : Merge
Usage : DataTable1.Merge DataTable2
Note : Sometimes we have 2 DataTables with same structure but different data , so we can use this method to combine the data into one DataTable (Both DataTable MUST have same structure , schema & constraint)


Copy DataRow from DataTable into another DataTable

Keyword : ImportRow
Usage : DataTable2.ImportRow (DataTable1.Rows(IndexOfRow))

Keyword : ItemArray
Usage : DataTable2.Rows.Add Row.ItemArray
Note : This one maybe more useful compare to ImportRow , when we have to loop for each row in DataTable , copy it into another DataTable without knowing the row's index

No comments: