comparing 2 DataRow.ItemArray.Clones

  • Thread starter Thread starter John B
  • Start date Start date
J

John B

Hello

On Form Closing I want to find out if the current datarow has been modifed.

I thought I could use the following code to get a copy of the record when
the form is loaded and a second copy in form closing and compare the two.
I am having problems comparing though. If I inspect each object, they appear
similar, but using == returns false. How do I do this?

object record = MyDataTable.Rows[0].ItemArray.Clone();

Thanks

John
 
This is a problem with reference equals. For reference types == means
evaluating if they point to the same reference pointer in the heap.
When you execute MyDataTable.Rows[0].ItemArray.Clone(), you are
creating a completely new object with a completely different address
space in memory. Cloning is used to allow modifying one instance
without affecting the other instance.

In your case, id suggest using the following code:

if (MyDataTable.Rows[0].RowState == DateRowState.Modified) {
//DO SOMETHING
}

If u want to use Clone, then you will have to compare the actual values
in the two different ItemArray and see if any value is different.
 
Back
Top