DataGridView not updating row contents when setting cell.Value

I ran across this last night when working on Todo 4.0 – apparently there is some sort of bug (or undocumented feature) that makes it so that when you update a cell’s value via the .Value property, the DataGridView itself is not refreshed. Adding or removing rows from a DataGridView causes it to be refreshed and redrawn, but if you edit just the contents of a single cell via the cell.Value property, it doesn’t refresh or redraw the grid. The actual value for the cell is updated, but the displayed value is not updated.

There seem to be a few common ways of fixing this:

  • Change the underlying DataSource for the DataGridView and call DataGridView.Refresh()
  • Call RefreshEdit() if the row has not already been added to the DataSource (as seen here)
  • Remove and re-add the row

I went with the latter. There’s probably a better way to do it, but I couldn’t figure out anything at the time (programming when tired can be a challenge).


One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s