Is there a way to know which data(row) dataadapter.update changed in the database?

I have a datagridview, changes in the gridview are updated with dataadapter.update at the database. But I have two databases so according to changes made in the datagridview the second table should be recalculated.

So I want to know which data is updated, inserted or deleted.

For example for people(id,name) table
when
1 John
2 Jack
3 Sawyer

is updated to
4 Sayid
2 Jack
3 Sawyer

I want to know that 1(id), John(name) changed.

Recommended Answers

All 3 Replies

There are several options:
You can implement your own update query. The default update query performs the update then performs a select to return the number of rows that were affected. By writing your own, you can replace this select to return the actual rows that were affected.

You can use the DataSet.GetChanges() method to view changes made.

Or you can use the OnRowUpdated event to check whcih values have changed and update your second table as necessary.

Thanks Ryshad I used GetChanges() and that works!

I had some problems using GetChanges(). I solved the problems, I am writing the solutions here for others having the same problem with me.

First I used DataSet.GetChanges(), it did not work with the datagridview so I used a datatable and set changes at the dataset to datatable.

datatable = new DataTable();
datatable = dataset.Tables[0].GetChanges();
dataGridView1.DataSource = datatable;

This code shows the changed (inserted and updated) rows at the datagridview however deleted rows are not shown.
So I searched a little and used this code to see updated(inserted, updated and deleted) lines in a listbox.

foreach (DataRow row in datatable.Rows)
                {
                    if (row.RowState == DataRowState.Deleted)
                    {
                        listBox1.Items.Add(row[0, DataRowVersion.Original]);
                        listBox1.Items.Add(row[1, DataRowVersion.Original]);
                    }
                    else
                    {
                        listBox1.Items.Add(row[0]);
                        listBox1.Items.Add(row[1]);
                    }
                        
                            
                }

Thanks again!

There are several options:
You can implement your own update query. The default update query performs the update then performs a select to return the number of rows that were affected. By writing your own, you can replace this select to return the actual rows that were affected.

You can use the DataSet.GetChanges() method to view changes made.

Or you can use the OnRowUpdated event to check whcih values have changed and update your second table as necessary.

no problem. Glad you got it working.
And thank you for posting the solution to the problems you have, they will help others who read this to solve the same problems.

Remember to mark the thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.