Search code examples
c#mysqldatagridviewrefreshdataadapter

Two datagridviews bound to the same table. Refresh not working


Situation:

I'm writing a Winforms app using C# in VS2013 with .NET4.0.

On a tabbed control I have two tabs.

  • In the first I have a dgv bound via a data adapter to a MySQL "product" table and also a dgv bound to a MySQL "stock" table. These are related so that only the stock items for a selected product are shown.
  • On the second tab I need to show a dgv with the full "stock" table, unconstrained by "product". So I've set up a separate adapter, filled from the same MySQL "stock" table. I use the separate adapter so that I get an unconstrained view of all the rows.

I also need to ensure that, where I make a change to the stock on one tab this gets reflected in the other tab.

Issue:

I update the database every time the user exits a dgv row (through adapter.Update on RowValidated). To ensure that changes are reflected in both tabs I handle the TabControl.Selected event where I (re)fill the associated adapter and then refresh the dgv. This however doesn't show the changes made in the other tab. I've ensured that, before switching tabs, the MySQL database has been updated correctly. It therefore seems that the other adapter or dgv are, for some reason not picking this up.

Question:

How can I achieve this refresh successfully? I'm thinking that this might be a simple issue of using the wrong commands or it might be that having two adapters pointed at the same table is the wrong approach.


Solution

  • It turned out that inserts and amendments were being reflected when I moved between tabs but not deletes. It then dawned on me that my (re)fill of the adapter would refresh amended rows and add new rows but it would leave removed rows untouched. So I simply inserted a DataTable.Clear before the Fill and that got it working just great.