Answered by:
Need Final DataTable object of ForEach loop DataTable object

Question
-
User339833461 posted
Hello Everyone,
In Foreach loop i am looping through the each object and getting each DatatTable object. So Finally I want to add these each DataTable into a final DataTable object. Here DataTable is of same type.
How to do it. any example, pls share me
Thanks,
Tuesday, May 5, 2020 10:09 AM
Answers
-
User288213138 posted
Hi Learning,
In Foreach loop i am looping through the each object and getting each DatatTable object. So Finally I want to add these each DataTable into a final DataTable object. Here DataTable is of same type.According to your description, I am not clear about your requirement.
Do you means you want to add a datatable to another datatable?
If so, you can try to use datatable.Merge() method.
DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt.Rows.Add(1, "name1", "age1"); dt.Rows.Add(2, "name2", "age2"); dt.Rows.Add(3, "name3", "age3"); DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt1.Rows.Add(4, "name4", "age4"); dt1.Rows.Add(5, "name5", "age5"); dt1.Rows.Add(6, "name6", "age6"); dt.Merge(dt1);
If you want to use the foreach loop to traverse the datatable, you can try belwo code:
DataTable dt = new DataTable(); foreach (DataRow row in dt.Rows) { }
If I misunderstand your requirement, please post more details information about your requirement.
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 6, 2020 2:22 AM
All replies
-
User415553908 posted
You could probably utilise DataTable.Load for this:
var dt1 = new DataTable(); dt1.Columns.Add("Col1", typeof(int)); dt1.Columns.Add("Col2", typeof(string)); var dt2 = dt1.Clone(); // just to ensure structure is identical. You will probably have different way to initialise your tables // fill both tables as you do // create result table with matching column layout var result = dt1.Clone(); result.Load(dt1.CreateDataReader()); // read first table into result result.Load(dt2.CreateDataReader()); // read second table into result
Tuesday, May 5, 2020 11:56 PM -
User288213138 posted
Hi Learning,
In Foreach loop i am looping through the each object and getting each DatatTable object. So Finally I want to add these each DataTable into a final DataTable object. Here DataTable is of same type.According to your description, I am not clear about your requirement.
Do you means you want to add a datatable to another datatable?
If so, you can try to use datatable.Merge() method.
DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt.Rows.Add(1, "name1", "age1"); dt.Rows.Add(2, "name2", "age2"); dt.Rows.Add(3, "name3", "age3"); DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt1.Rows.Add(4, "name4", "age4"); dt1.Rows.Add(5, "name5", "age5"); dt1.Rows.Add(6, "name6", "age6"); dt.Merge(dt1);
If you want to use the foreach loop to traverse the datatable, you can try belwo code:
DataTable dt = new DataTable(); foreach (DataRow row in dt.Rows) { }
If I misunderstand your requirement, please post more details information about your requirement.
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 6, 2020 2:22 AM