DataRow Replacement

  • Thread starter Thread starter Val
  • Start date Start date
V

Val

Hi all!

Problem:
Iv copied a DataRow from a DataSet and passed it to a function to be edited.
How do I put it back to the DataSet/DataTable? Every way I tried it says
"record already exists" becouse they have same primary keys, is there a
function that updates/overwrites an existing DataRow with a new one?

Thanks!
 
Val,
Why don't you simply pass the DataRow from a DataTable? Instead of "copying"
it.

Something like:

Dim table As DataTable
For Each row As DataRow in table.Rows
MyMethod(row)
Next

Public Sub MyMethod(ByVal theRow As DataRow)
theRow.Item("Field1") = "Silly Me"
End Sub

The above will modified every row to have "Silly Me" in the Field1 field.

You can use DataTable.ImportRow to import a DataRow into a table while
checking for duplicate keys. However I would not use ImportRow on a row from
that table, I would use it to import rows from a second table into the first
table.

Hope this helps
Jay
 
Thanks for the reply Jay,
The "function" is on another form (Form2) and to pass it there i used a
public DataRow (publicDataRow),is this a stupid way? I tried other ways to
pass stuff between forms but it kept coming up wiv errors. So anyway...Form1
copies a record out of its dataset to Form2.publicDataRow, Form2 then lets
user edit the data. Then im trying to make Form1 copy the data back to its
dataset from Form2.publicDataRow.
Thanks again!
 
Sorry for being shuch a noob, there never was a problem.....
C# never actualy copies a record, it sends a pointer or something.
Oh god wot a noob!
Thanks!
 
Val,
I was concerned about that. :-|

Remember that DataRow is a Reference Type which means that when you assign a
DataRow variable to another DataRow variable a copy of the reference is made
not a copy of the DataRow itself. There is only ever one instance of that
specific DataRow object.

For an explanation of Reference Types, Value Types and how the act as
parameters see:

http://www.yoda.arachsys.com/csharp/parameters.html

Hope this helps
Jay
 
Back
Top