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.
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
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
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
8 Points
20 Posts
Need Final DataTable object of ForEach loop DataTable object
May 05, 2020 10:09 AM|Learning Rocks|LINK
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,
Member
160 Points
96 Posts
Re: Need Final DataTable object of ForEach loop DataTable object
May 05, 2020 11:56 PM|timur.kh|LINK
You could probably utilise DataTable.Load for this:
Contributor
3370 Points
1409 Posts
Re: Need Final DataTable object of ForEach loop DataTable object
May 06, 2020 02:22 AM|samwu|LINK
Hi Learning,
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:
If I misunderstand your requirement, please post more details information about your requirement.
Best regards,
Sam