undo/redo in TAdvStringGrid

Can someone provide me an example of the use of the undo and redo feature? The developers guide and the demos do not seem to give any working examples.

Thanks,

Jim

Drop a button, grid and AdvGridUndo component on the form and add the code:


procedure TForm5.Button1Click(Sender: TObject);
begin
  AdvGridUndoRedo1.Undo;
end;

procedure TForm5.FormCreate(Sender: TObject);
begin
  AdvStringGrid1.Options :=  AdvStringGrid1.Options + [goEditing];
  AdvStringGrid1.UndoRedo := AdvGridUndoRedo1;
end;

Then edit a cell value and after this, press Undo and you will see that the editing is undone.

Hi,

Undoredo works fine for editing cells.
Is there also a (total) solution for undo other changes in the grid like: insert row, removerow, move row,...

The built-in undo/redo is for editing actions in cells.
As typically, insert/remove rows is under programmatic control, if you want to undo this, you'd need to handle this at application level, i.e. store state before doing such insert/remove and then add the ability to restore the state.

So, in that case undo/redo is not wath we are looking for to use as undo/redo.
In this case the application is an excel-like editor for the customer to change files. Allowing to insert, move, delete rows, edit cells,... We really need to make here undo/redo for.
So we have to build ourselves a stack with all (some) previous steps. Is there a 'quick' property we can use to save a 'state' of the grid?

1 Like

You could take a snapshot / restore a snapshot with grid.SaveToStream(), grid.LoadFromStream() or grid.SaveToBinStream(), grid.LoadFromBinStream() in case your grid cells use other than data also properties (like color/alignment/font)

This trick solved the problem! Thanks for the quick feedback.

"Strange that this is not a standard functionality in the grid .. seems useful in several applications.
The biggest problem is that we now have to do an AddUndoStep manually in all kinds of places."

As this can potentially start consuming quite some memory, it is not a standard operation.

We have discovered a very annoying problem in this solution.
Cells with decimals (.) lose the . when retrieving from stream.

Example:
procedure TForm1.ButtonTestUndoRedoStreamClick(Sender: TObject);
var
r, c: Integer;
sUndoRedo: TObjectList;
begin

sUndoRedo := TObjectList.Create;
try

sUndoRedo.Add(TMemoryStream.Create);

with AdvStringGrid1 do
begin
  FixedRows := 1;
  RowCount := 5;

  for r := 1 to RowCount - 1 do
    for c := 1 to colcount - 1 do
      Cells[c, r] := c.ToString + '.' + r.ToString;

  sUndoRedo.Items[0].Position := 0;
  SaveToBinStream(sUndoRedo.Items[0]);

  // some actions or code
  sUndoRedo.Items[0].Position := 0;
  LoadFromBinStream(sUndoRedo.Items[0]);

end;

finally
freeandnil(sUndoRedo);
end;

end;

This code doesn't work. You'll need to format this as a code block.
Or better, please provide a sample source project with which we can reproduce the problem.

We solved it.
Before saving to stream we put the columns between " ". And after restoring the " " will be removed. This immediately solved the problem.

It remains strange though. If you have the time to prepare something we can reproduce, we'll investigate.