Forcing a virtual DataGridView to refresh its Cell values RRS feed

  • Question

  • I have a virtual DataGridView that has a data cache behind it.

    Sometimes I update the values in the cache programatically and I want to be able to tell the DataGridView to refresh the cells.
    That is, for it to trigger the CellValueNeeded event.

    Currently I just make a call to DataGridView.Refresh() which invalidates the entire DataGridView and repaints it - which includes triggering the CellValueNeeded event.

    There are problems with this though. Firstly, it seems a somewhat brute force method. I don't really need to repaint the whole thing...
    Secondly, I have had intermittent Nullreference exceptions down in the GDI layer when I do a refresh. Not fun.

    Anyone have any ideas?


    Thursday, November 24, 2005 4:23 AM


  • Use the InvalidateCell, InvalidateRow or InvalidateColumn calls for a more specific update.



    DataGridView Program Manager
    This post is provided "as-is"


    Thursday, December 1, 2005 6:01 AM

All replies

  • I encountered the same problem recently, and my workaround was to call the Invalidate() method on the DataGridView after changes to the cache.

    In virtual mode this will cause all cells in the DataGridView to raise the CellValueNeeded event and retrieve all the data from the cache/datastore.  You can abstract this further by raising events on your datastore when the internal state changes etc.

    I am eager to hear if there is a better way to acomplish this in .NET, perhaps something like the DefaultTableModel , or AbstractTableModel that Java uses.  

    Thursday, December 1, 2005 2:55 AM
  • Use the InvalidateCell, InvalidateRow or InvalidateColumn calls for a more specific update.



    DataGridView Program Manager
    This post is provided "as-is"


    Thursday, December 1, 2005 6:01 AM
  • I have a question along similar lines. I actually used the Refresh() method, which as I understand it invalidates and thus repaints the entire DataGridView. The problem, however, is that I wish to refresh all the way to the database (i.e. re-execute the underlying query). How is this done?
    Tuesday, January 10, 2006 7:37 PM
  • You need to re-databind the entire grid and call Fill again on the table adapter (if you are using it)



    DataGridView Program Manager


    This post is provided "as-is"


    Tuesday, January 10, 2006 9:15 PM
  • I was not sure how to re-databind as you suggested, so I simply re-executed the TableAdapter Fill() and that seems to be sufficient. Thanks!
    Tuesday, January 10, 2006 9:34 PM
  • Yes, I have used both InvalidateCell and InvalidateRow and the entire datagrid still flickers upon issuing Refresh(). I was under the impression that only the area of the cell or row would be refreshed.

    Tuesday, October 10, 2006 5:07 PM
  • I think the point is that you are meant to use InvalidateCell *instead* of using Refresh.
    Tuesday, October 10, 2006 7:40 PM
  • I think RefreshEdit is what you are looking for.
    • Proposed as answer by Jake Fosheezee Thursday, March 24, 2011 1:52 PM
    Tuesday, April 17, 2007 7:36 PM
  • you are looking for invalidate()
    • Edited by pilouk Thursday, November 21, 2013 7:48 PM
    Thursday, November 21, 2013 7:48 PM
  • I arrived at this page while looking for a way to get the displayed state of a grid view checkbox cell after the user had just clicked on it and changed it from checked to unchecked or from unchecked to checked, but before the user had moved the focus to a different cell. (I needed to update another part of the view immediately.) My problem was that the cell value continued to reflect the previous state until that cell lost focus. I tried Invalidate, InvalidateCell, UpdateCellValue, Refresh, RefreshEdit, everything I could think of. Nothing worked. Finally, I programmatically set the focus to a different cell and then back again, using CurrentCell. That seems brutish to me, but it was the only way I found to make it work.

    Tuesday, September 26, 2017 9:22 PM
  • Great!
    Wednesday, July 22, 2020 7:52 PM