Search code examples
vb.netdatatabledatagridviewexpressionrefresh

How can I force a DataGridView to redraw or refresh after any front end changes to the data?


I have a DataGridView which is databound to a DataTable. The DataTable is originally populated by a MySQL database, but the connection is not live. This DataTable(tblorderspecs) has several columns which are automatically calculated through the .Expression method. Only one of these columns is visible on the DataGridView.

tblorderspecs.Columns("Total").Expression = "Convert(Quantity * Cost - BaseTotal + .499999, 'System.Int64')"

The user is able to manually edit the quantity and cost columns of the DataGridView, which in turn updates tblorderspecs, which in turn triggers the calculation of the "Total" column of tblorderspecs, which in turn is reflected in the DataGridView.

Here is my problem. The DataGridView does not always repaint/redraw/refresh when this series of events occurs. This is an intermittent issue. I have used the debugger and breakpoints to determine that all of the steps are working properly. If I exit the DataGridViewRow after editing the Quantity or Cost column, everything works as it should and the DataGridView reflects the changes. If I merely exit the cell after editing the Quantity or Cost column and remain on the same row, I have about a 50/50 chance of the DataGridView showing the updated information. I have used breakpoints and the debugger to attempt to figure out what is going on. Using the immediate window, I have found that the DataGridView cell value is properly updating, even when the screen continues to show old data. I repeat, the DataGridView holds the new data, but the screen does not reflect the update. This indicates to me that this is a redrawing/repainting/refreshing issue on the user interface.

As a first step, I tried using the refresh method on the DataGridView.

DataGrid.Refresh()

This does indeed resolve my problem, but it makes the program unusable. There is about a .25 second delay every time I call this method, which is not practical. I can't possibly be satisfied with that kind of lag every time a user moves between cells. I will also note, that when the DataGridView automatically refreshes, such as when I move between rows, I experience no perceptible lag. So the automatic refresh is more efficient then the manual refresh.

I am hoping that at least one of the three solutions I have listed below is feasible and someone can help me achieve them or provide another solution. Thank you.

  1. Whatever repaint/redraw/refresh method which is automatically called anytime a user switches rows can be set to be called anytime any data is changed in the DataGridView.

  2. Whatever repaint/redraw/refresh method which is automatically called anytime a user switches rows can be manually called.

  3. Instead of refreshing the whole DataGridView using the .Refresh() method, perhaps there is a way to refresh a single cell or row. Something like DataGrid.CurrentRow.Cells("Total").Refresh().

Note, I am not sure what other code might be helpful/relevant for me to upload. It is a large project. Thank you for your help.


Solution

  • I was able to solve this issue with the DataGridView.Invalidate() method. This forces the DataGridView to redraw but it does not cause all of the slow downs of the .Refresh() method. I placed code to Invalidate any time the underlying Datatable was changed.