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.
Unfortunately, activation email could not send to your email. Please try again.

Copying DataRow of DataBound DataTable

Thread ID:

Created:

Updated:

Platform:

Replies:

7300 Sep 18,2003 07:32 PM Sep 19,2003 09:43 AM Windows Forms 1
loading
Tags: FAQ
Mike
Asked On September 18, 2003 07:32 PM

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

Mike
Replied On September 19, 2003 09:43 AM

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;