We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Copying DataRow of DataBound DataTable

Here is my scenario: I have a DataTable with EXACTLY 1 row in it and many controls on my form have been databound to this DataTable. I would like to get another DataTable that is EXACTLY the same in structure with EXACTLY 1 row, but different data and move the new data into the original data keeping all my controls bound to the original DataTable. I am currently binding and and then clearing the bind everytime I get a new record and i would like to try and avoid this bind over and over again. It should only need to be bound to the DataTable once, and then I want to update the row it is bound too, from another DataTable. Can this be done with some nice copy or clone features with the DataSet's and DataTable's, or would I have to write a routine that walks the columns and moves the value at row[0][columnPosition] in a brute force way. I've never been able to successfully take a row from one datatable and add it to another, it always complains that the datarow belongs to another datatable. It would be really slick if I could do this. The key part is that binding would stay on the entire life of the form - when new data is called, it simply updates(replaces) the current values and still stays bound.... any help would be appreciated Thanks Mike

1 Reply

MI Mike September 19, 2003 01:43 PM UTC

I figured out how to do this like this: for (int i = 0;i < dt1.Columns.Count;i++) { dt2.Rows[0][i] = tableToBind.Rows[0][i]; } Is this the best way, any faster more efficient ways? Also - I'm doing this to avoid rebinding where I have to call DataBindings.Clear() on each of my binded controls in a foreach loop through an arraylist. Does anyone know which is faster: A) Bind every time by clearing the binding and rebinding the controls in a foreach loop testing each type of control in a switch statement B) Bind it once - leave the controls on a page bound, and use the for loop above to simply update the values of the dataset and the controls reflect the changes as they are ALWAYS bound from the initial bind thanks

Live Chat Icon For mobile
Up arrow icon