Search code examples
c#datagridviewbindingsourcetableadapter

Refresh efficiently DataGridView after updating row


I have a DataGridView that gets the data from SQL Server with the help of a TableAdapter and a BindingSource.

I have a method that detects changes to the rows and sends an update query to SQL Server. This works perfectly.

After that, to update the DataGridView, I do the following:

this.TableAdapter.Fill(this.DataSet.DataTable);

The problem is that this takes a long time due to the number of rows.

I need to refresh it because the table adapter is a view that merges two tables with a left join.

Is there a way to update only one row or other faster way to update the view?


Solution

  • Make another query on the Tableadapter (right click it, add query) that takes a parameter of the data to fetch. For example if you have an existing query:

    SELECT * FROM a LEFT JOIN b ON a.id = b.id
    

    (note; it is generally a really bad idea to write queries in tableadapter a that download the entire database contents into the client)

    You can add another query to the tableadapter:

    SELECT * FROM a LEFT JOIN b ON a.id = b.id WHERE a.id = @aid
    

    And call it FillByAId/GetDataByAId

    Then in the client side you can, after updating some row ID 1234:

    var newDt = yourTableAdapter.GetDataByAId(1234);
    

    And then merge the changes into your existing datatable with datatable.Merge