views:

135

answers:

3

I'm trying to add some extra logging to my C# winforms application. I have a few data bound forms that all the database stuff is managed by a binding source and some typed datasets/adapters.

With this setup, it's kind of difficult to tell when something is changed, I'd have to manage each field and keep it's previous value. Is there a way I can hook into the dataset and tell when something is changed? I know datarow's have a RowState enumeration, would that be a good place to start? I looked into the binding source's DataMemberChanged event but it never fired so...

+2  A: 

You can use the RowChanged event, the RowChanging event, or any of the other events raised by a DataTable.

David Stratton
Will these also give me the previous value(s)? Or is that too much to ask?
Malfist
I don't think it will. I know that you can roll back the changes by calling RejectChanges to restore the row to its previous value.
David Stratton
Can I tell which column changed?
Malfist
I could do a quick query to the database to grab the row and check it..., that might be expensive though. And may not be fool proof if the update query executes first.
Malfist
+2  A: 

To know what has changed look at the DataSet.GetChanges method. The example shows how to get the changes and go through them. I also have an old example here that uses a DataTable and shows how to do a comparison after a merge. It inspects the RowState and shows the changed values etc. It's near the bottom of the page and it's in VB since the OP was using that, not C#. I'm headed out now so I can't provide an equivalent translation but it should be pretty straightforward to glean some useful techniques from.

Ahmad Mageed
+2  A: 

To get the original value of an updated Data value you can do this:

<DataTableRow>[<DataColumn>, DataRowVersion.Original]
CSharpAtl
I wish I could split the credit for the right answer...Thank you!
Malfist
That's great information. THANK YOU! I'll use that myself. I wish I could vote you up more than once for that.
David Stratton
Glad I could help....
CSharpAtl