An exception-safe way to change read-only cells
If you are executing operations that might throw exceptions you might consider a more exception-safe way. Declare a class that unlocks the grid in the constructor and locks it again in the destructor.
class CGridUnlock
{
public:
CGridUnlock (CGXGridWnd* grid, BOOL bUnlock = TRUE)
: pGrid(grid) { if (bUnlock) Unlock(); }
~CGridUnlock() { Lock(); }
void Unlock() { pGrid->SetReadOnly (FALSE); }
void Lock() { pGrid->SetReadOnly (TRUE); }
CGXGridWnd* pGrid;
};
To use this, just declare a CGridUnlock object at whenever you want to unlock the grid. The grid will lock again when the class goes out of scope. If you want finer control, then call the Lock/Unlock methods of the class. If an exception is hit and the catch is outside the scope of the object, then the destructor will lock the grid again.