Board index » delphi » TDBGrid onvalidate - what am i doing wrong

TDBGrid onvalidate - what am i doing wrong

Hi.

I'm looking for a good example of performing validation on a cell
in a TDBGrid. The following code prevents invalid entry but
is quite confusing in it's behaviour as described below.

procedure TDataModule1.Table1FredValidate(Sender: TField);
begin
if ((Sender as Tfield).value = 'Bad')
then
  begin
    MessageDlg('No Bad value please', mtInformation,
      [mbOk], 0);
    Table1.abort;
  end;
end;

During the OnValidate and after clicking the message box OK the cell in
error remains the selected cell but reverts to its previous valid value.
However the Edited symbol in the indicator column remains.

If the user clicks in the field to change the value the field
now displays the invalid value and immediately repeats the validation
which fails.

This cycle repeats ...

Editing the cell directly without attempting to click into it
works ok.

Ideally I would like the cell value to remain the invalid value and only
repeat the validation if the user did non attempt to amend the value. If I
click into the cell then give me a chance to change the value.

 

Re:TDBGrid onvalidate - what am i doing wrong


Quote
Allen Davidson wrote:

> Hi.

> I'm looking for a good example of performing validation on a cell
> in a TDBGrid. The following code prevents invalid entry but
> is quite confusing in it's behaviour as described below.
> Could this be the code you are looking for? It is taken directly

from the Delphi MASTAPP example.

procedure TOrderForm.ItemsPartNoValidate(Sender: TField);
begin
  if not Parts.FindKey([ItemsPartNo]) then
    raise Exception.Create('You must specify a valid PartNo');
end;

Other Threads