I have a DataGridView and I want to show the operator which values are changed by giving those values a different ForeColor. The operator can decide to discard all changes by clicking on a Discard button. In that case I need to let the cell use the inherited styles.
My problem is that once I created a CellStyle to indicate that it is changed I can't undo this so that the cell uses the inherited style.
I did some research. In the article Cell Styles in the Windows Forms DataGridView Control MSDN warns:
Caching the values stored in the cell's Style property is important regardless of whether a particular style value is set. If you temporarily replace a style setting, restoring it to its original "not set" state ensures that the cell will go back to inheriting the style setting from a higher level.
Alas, this doesn't seem to work:
DataGridViewCell cell = ...;
Debug.Assert(!cell.HasStyle); // cell is not using its own style
var cachedColor = cell.Style.ForeColor; // cache the original color
cell.Style.ForeColor = Color.Red; // indicate a change
Debug.Assert(cell.HasStyle); // now cell is using its own style
// restore to the 'not set' state:
cell.Style.ForeColor = cachedColor;
Debug.Assert(!cell.HasStyle); // exception, not using inherited style
cell.Style = cell.InheritedStyle; // try other method to restore
Debug.Assert(!cell.HasStyle); // still exception
So question: How to restore the style setting to its original "not set" state?