How to merge two datatables in ASP.NET
Another post, just because of the continuous query in forums. Past one week I saw 3 candidate asking the same question so I thought I will share this code.
private static DataTable MergeDT(DataTable Dt1, DataTable Dt2)
{
if (Dt1 != null && Dt1.Rows.Count > 0)
{
DataRow dr;
for (int i = 0; i < Dt2.Columns.Count; i++)
{
Dt1.Columns.Add(new DataColumn(Dt2.Columns[i].ColumnName));
if (Dt2.Rows.Count > 0)
for (int j = 0; j < Dt2.Rows.Count; j++)
{
dr = Dt1.Rows[j];
dr[Dt1.Columns.Count - 1] = Dt2.Rows[j][i];
dr = null;
}
}
}
return Dt1;
}