Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How can I populate datatable2 (dt2) from the first datatable1 (dt1), but I do not want dt2 to be dependant of dt1.
For example:
I am populating dt2 from dt1. But if the data in dt1 changes, so does dt2.
I do not want dt2 to change.
How can I do this for dt2 please?
Thanks

i.e.
datatable dt1 = ds.table[0]...
...

//copy the distinct field to dt2
DataTable dt2 = dt.DefaultView.ToTable(true, "FieldName");
do I have to do anything with Binding context for dt2 ?

Thanks
Posted
Updated 3-Feb-12 4:52am
v2
Comments
arkiboys 3-Feb-12 10:53am    
Hi Sridhar Patnayak4
Did you post anything?

Just use DataTable's Clone property[^] and then repopulate the table.
 
Share this answer
 
Comments
arkiboys 3-Feb-12 11:07am    
Can I do this?
DataTable dt2 = dt.DefaultView.ToTable(true, "FieldName");
DataTable dtFieldName = dt2.Clone();

lstFieldName.Items.Clear();
foreach (DataRow dr in dtFieldName.Rows)
{
lstFieldName.Items.Add(dr["FieldName"].ToString());
}
Manfred Rudolf Bihy 3-Feb-12 11:10am    
After reading your solution I had to revise mine as I tried to go with DataTable.Copy(). Good point and a well deserved 5!

Cheers!
Abhinav S 3-Feb-12 23:22pm    
Thank you.
The way you are using it the DataTable (or at least the DataRows) is(are) in reality the same, identical object(s). If you do want a copy to work on that will not behave "synchronized" use the method DataTable.Clone();. See here: http://msdn.microsoft.com/en-us/library/system.data.datatable.clone.aspx[^].

C#
DataTable dt1 = ds.table[0];
DataTable dt2 = dt1.Clone();
// Populating the cloned DataTable is left as an excercise to OP :)


Regards,

Manfred
 
Share this answer
 
v4
Comments
arkiboys 3-Feb-12 16:34pm    
Thanks

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