Reputation: 4524
I have a datatable dt1 with 4 column(Name,Address,Contact,Marks) which has 0 Rows, and I have another datatable dt2 with 2 column(StdName, StdAddress) with few Rows.
when I am doing something like dt1 = dt2.copy(); then my dt1 is getting change. which I don't want.
I want to copy dt2 column and value to dt1. so in my dt1's column(Name and Address) will fill with stdName and StdAddress Value.
Please someone help me how to do that.
Upvotes: 3
Views: 35004
Reputation: 173
You can use the expression property if a calculated value is enough for you without looping the whole datatable.
Assuming you already have a column named Email, you can create a column named UserName derived from Email:
dataTable.Columns.Add("UserName", typeof(string), "Email");
Upvotes: 0
Reputation: 84
You can clone the Reference to the Table and delete Rows by call clear like so:
NewDataTable= OldDatatable.Clone();
NewDataTable.rows.clear();
Upvotes: 0
Reputation: 21
for (int k = 0; k < dt2.Rows.Count; k++) {
dt1.Rows[k]["Name"] = dt2.Rows[k]["StdName"];
dt1.Rows[k]["Address"] = dt2.Rows[k]["StdAddress"];
}
This worked excellent for me. I just had to rewrite it in VB like this:
For r = 0 To t2.Rows.Count - 1
t1.Rows(r)("Stat") = t2.Rows(r)("Stat")
t1.Rows(r)("Dest") = t2.Rows(r)("Dest")
t1.Rows(r)("Orig") = t2.Rows(r)("Orig")
Next
Upvotes: 0
Reputation: 21
I am not sure but you can try this....
Datatable1.Merge(Datable2)
It will give you output like this
Upvotes: 0
Reputation: 81
I know this is older, but where is the data coming from?
If you are querying a sql database, you can just change:
SELECT <column1>, <column2> FROM...
SELECT <column2>, <column1> FROM...
Query and return a datatable with the columns like you want them.
Upvotes: 0
Reputation: 123
try it....
DataTable dt1 = new DataTable();
dt1.Columns.Add("col1", typeof(String));
dt1.Columns.Add("col2", typeof(String));
dt1.Columns.Add("col3", typeof(String));
dt1.Columns.Add("col4", typeof(String));
DataTable dt2 = new DataTable();
dt2.Columns.Add("col5", typeof(String));
dt2.Columns.Add("col6", typeof(String));
dt1.Merge(dt2);
Upvotes: 0
Reputation: 4273
Try this:
for (int i = 0; i < dt2.Rows.Count; i++)
{
dt1.Rows.Add(dt2.Rows[i][0], dt2.Rows[i][1]);
}
Upvotes: 3
Reputation: 1560
for (int k = 0; k < dt2.Rows.Count; k++)
{
dt1.Rows[k]["Name"] = dt2.Rows[k]["StdName"];
dt1.Rows[k]["Address"] = dt2.Rows[k]["StdAddress"];
}
Upvotes: 1
Reputation: 4524
I tried with this solution
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);
}
}
CopyColumns(source, destiny, "Column1", "column2");
It help me to solve my problem.
Upvotes: 6
Reputation: 1402
Try this.
DataTable dtEmp = new DataTable("EMP");
dtEmp.Columns.Add("Name", typeof(String));
dtEmp.Columns.Add("Address", typeof(String));
dtEmp.Columns.Add("Contact", typeof(String));
dtEmp.Columns.Add("Marks", typeof(String));
DataTable dtStd = new DataTable("STD");
dtStd.Columns.Add("StdName", typeof(String));
dtStd.Columns.Add("StdAddress", typeof(String));
foreach (DataColumn dc in dtStd.Columns)
{
dtEmp.Columns.Add(dc.ColumnName, dc.DataType);
}
// Import rows from dtStd Table.
Upvotes: 0