Force and Edited Cell to keep focus if not valid

I try to implement a grid where an edited cell behave as follow:
- before exiting an edited  cell, the grid check if this cell match some constraints.
- If the value match the constraints you can leave the cell.
- if the value doesn't match the constraints, the cell MUST keep the focus.

For my example, I build a grid (2x2) where when Col=1 I have an edNumeric editor Type.
On the CellValidate event, I have this code :
//------------------------------------------------------------------------------
procedure TForm2.GridCellValidate(Sender: TObject; ACol, ARow: Integer;
  var Value: string; var Valid: Boolean);
//------------------------------------------------------------------------------
var
  i:Integer;
begin
  if Value='' then valid:=True
  else begin
    i:=StrToInt(value);
    case aRow of
      0: Valid:= (i mod 2)=0;
      1: Valid:= (i mod 2)=1;
    end;
  end;
end;

Which set valid to True if you type an even value for row 0 and an odd value for row 1.

When I try to leave my edited cell with Up/down key and this code set Valid variable to False. The Cell keep the focus and I remain in the cell. 
But If i click into another cell, and Valid is false, my grid cancel the edit AND exit the cell.

You can block the mouse to move the focused cell by implementing OnSelectCell.

Example:


Add at form level a variable lastval to store the last validation result.

procedure TForm3.AdvStringGrid1CellValidate(Sender: TObject; ACol,
  ARow: Integer; var Value: string; var Valid: Boolean);
begin
  if Length(Value) > 2 then
    valid := false;
  lastval := valid;
end;

procedure TForm3.AdvStringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  CanSelect := lastval;
  if not lastval then
    AdvStringGrid1.ShowInplaceEdit;
end;