Hi Shwetha,
Unfortunately there is no direct method implemented for cloning dataColumns. But for your challenge, two possible solutions pop to mind.
1. After creating the column, loop through all rows to copy the data from the source to the target.
2. Do a datatable1.Copy() to copy all columns+data and delete the ones you don't need.
I've included a sample for solution 1, as solution 2 is pretty self explainatory. First create a method that allows you to copy content of datacolumns by iterating through the datarows in that column.
private void CopyColumns(DataTable source, DataTable dest, params string[] columns)
{
foreach (DataRow sourcerow in source.Rows)
{
DataRow destRow = dest.NewRow();
foreach(string colname in columns)
{
destRow[colname] = sourcerow[colname];
}
dest.Rows.Add(destRow);
}
}
You can call this method by using : ( naming any column )
CopyColumns(source, destiny, "Column1", "column2");
Or in your particular example that would be :
CopyColumns(tb1, tb3, "a");
CopyColumns(tb2, tb3, "c");
Don't forget to create the columns in tb3 with the right properties first, or the method will fail. An alternative would be looping through the rows in the tables :
private void CopyColumn(DataTable srcTable, DataTable dstTable, string srcColName, string dstColName)
{
foreach (DataRow row in srcTable.Rows )
{
DataRow newRow = dstTable.NewRow();
newRow[dstColName] = row[srcColName];
dstTable.Rows.Add(newRow);
}
}
Important : Don't forget to create the columns in tb3, or both methods will fail.
Cheers,
Rick