Hi.
Having a grid with AppendOnArrowDown set to true I can use OnCanAddRow event, to make sure the user has input enough data to allow a new row to be appended.
This works OK - but I need to remove then newly inserted row if the user chooses to "arrow up" , tab to next control or just click on another row in the grid.
Checking with OnRowChanging or OnRowUpdate I can decide if the newly inserted row is obsolete ... but I cannot use DeleteRows to get rid of it. This will start an endless loop calling OnRowChanging or OnRowUpdate multiple times.
How can I achieve this simple task?
RemoveRows .... not DeleteRows :-)
procedure TForm1.AdvStringGrid1RowChanging(Sender: TObject; OldRow, NewRow: Integer; var Allow: Boolean);
begin
if (AdvStringGrid1.Cells[1, OldRow] = '') and // first check ...
(AdvStringGrid1.Cells[2, OldRow] = '') then
begin
if AdvStringGrid1.Cells[1, NewRow] <> '' then // second check - if needed.
begin
AdvStringGrid1.OnRowChanging := nil;
AdvStringGrid1.RemoveRows(OldRow,1); // RemoveRows to cancel appended or inserted row.
AdvStringGrid1.OnRowChanging := AdvStringGrid1RowChanging;
end;
end;
end;
Not the best solution - but it will do the trick.
OnRowChanging is triggered while a row is about to be changed so it is expected this will cause problems when you execute code that affects rows from OnRowChanging. Temporarily removing the event handler is indeed a possible solution (or setting a global flag to stop further processing of the event)
Yes it works - and is needed. The idea of a global flag is a solution - but I think I'm better off with this.
Suggestion:
Stopping even handlers could be built into BeginUpdate / EndUpdate.
Or as others do - implement a new method - BeginFullUpdate / EndFullUpdate.