Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I wanna copy column "a" from tb1 and column "c" from tb2 to table tb3. length of column "a" and column "c" are different. tb1 and tb2 DataTables are in a DataSet ds11.

Please help me out thanks in advance.
Posted
Updated 18-Jan-11 1:05am
v2

Something like this
DataTable dt1 = new DataTable();
DataColumn column1 = new DataColumn("First Column");
dt1.Columns.Add(column1);
DataTable dt2 = new DataTable();
DataColumn column2 = new DataColumn("First Column");
dt2.Columns.Add(column2);

DataTable dt3 = new DataTable();
dt3.Columns.Add(dt1.Columns[0]);
dt3.Columns.Add(dt2.Columns[0]);
 
Share this answer
 
Comments
shwetha_m6 20-Jan-11 5:22am    
Thanks for your response, but its not yet solved i'l post my code here can u help me out pls..!! Cz its giving error saying Data column already exist in another table.


DataSet ds11 = new DataSet();
DataTable tab = new DataTable();
try
{
con.Open();
//selecting assign id and resource name where mod id matches.
com = new SqlCommand("Select assign_Id, mod_Assign from Assign_Module where mod_Id=@mid", con);
com.Parameters.Add("@mid", mid);
ra = com.ExecuteNonQuery();
da = new SqlDataAdapter(com);
da.Fill(ds11, "ResMon");
//dt = ds11.Tables["ResMon"];
if (ds11.Tables["ResMon"].Rows.Count>0)
{
int aid;
//taking assign id from table which is in dataset.
aid = Convert.ToInt16(ds11.Tables["ResMon"].Rows[0][0]);
com = new SqlCommand("Select month_Assign from Assign_Month where assign_Id=@aid", con);
com.Parameters.Add("@aid", aid);
ra = com.ExecuteNonQuery();
da = new SqlDataAdapter(com);
da.Fill(ds11, "Month");
tab.Columns.Add(ds11.Tables["ResMon"].Columns[1]);
tab.Columns.Add(ds11.Tables["Month"].Columns[0]);
}
con.Close();
return tab;
}
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.

C#
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 )

C#
CopyColumns(source, destiny, "Column1", "column2");


Or in your particular example that would be :

C#
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 :

C#
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900