C#

<pre> public DataTable Merge(DataTable[] dataTables) { List<int> oList = new List<int>(); DataTable mergedDataTable = new DataTable(); foreach (DataTable dt in dataTables) { oList.Add(dt.Rows.Count); foreach (DataColumn dc in dt.Columns) { mergedDataTable.Columns.Add(dt.TableName + "-" + dc.ColumnName, dc.DataType); } //mergedDataTable.Columns.Add(dt.TableName + "-" + "Space"); } int temp = 0; for (int m = 0; m < oList.Count; m++) { for (int n = 0; n < oList.Count - 1; n++) { if (oList[n] > oList[n + 1]) { temp = oList[n + 1]; oList[n + 1] = oList[n]; oList[n] = temp; } } } int maxRow = oList[oList.Count - 1]; for (int o = 0; o < maxRow; o++) { DataRow newRow = mergedDataTable.NewRow(); int mergedDataTableColumn = 0; foreach (DataTable dt in dataTables) { if (dt.Rows.Count > o) { for (int k = 0; k < dt.Columns.Count; k++) { newRow[mergedDataTableColumn] = dt.Rows[o][k]; mergedDataTableColumn++; } } else { for (int k = 0; k < dt.Columns.Count; k++) { newRow[mergedDataTableColumn] = DBNull.Value; mergedDataTableColumn++; } } //newRow[mergedDataTableColumn] = DBNull.Value; //mergedDataTableColumn++; } mergedDataTable.Rows.Add(newRow); } return mergedDataTable; }