How to get the value that caused the TDBGridInplaceEdit error?

1.1k views Asked by At

I'm trying to write an exception handler that displays user friendly messages. I don't know how to get the "Newly Entered" data value that caused the TDBGridInplaceEdit error.

For example:
I have a DBGrid loaded with data. When I intentionally change the PartNo field of row #1 to a non numeric value to cause a TDBGridInplaceEdit error... (From: 1313.3 To: 1313..3) ... I trap the error and display a message but I can't figure out how to get the bad '1313..3' value.

enter image description here
Original PartNo: 1313.3

enter image description here
Changed PartNo: 1313..3 (two decimal points)

enter image description here
Displayed Error Message from the Application onException

procedure TMain.ApplicationEvents1Exception(Sender: TObject; E: Exception);
var
  str : string;
begin
  str := sender.ToString;
  str := str + #10;
  str := str + RzDBGrid2.SelectedField.FieldName;
  str := str + #10;
  str := str + VarToStr(RzDBGrid2.SelectedField.Value);
  str := str + #10;
  str := str + e.Message;
  showmessage(str);
  //Application.ShowException(E);
end;

I'd like to format my own message using the bad '1313..3' value that was entered. How do you get this value?

1

There are 1 answers

2
bummi On BEST ANSWER

If you are persisting the fields of your dataset, you can define an OnSetText Method on your fields.(Doubleclick on the dataset and choose add fields).

enter image description here

The Method could look like this:

procedure TForm1.ADataSetAFloatFieldSetText(Sender: TField; const Text: string);
var
 f:Double;
begin
  if not TryStrToFloat(Text,f) then
      begin
       raise Exception.Create(
                              'Error on: '
                              + #13#10'Dataset: '   + Sender.DataSet.Name 
                              + #13#10'Field: '     + Sender.FieldName 
                              + #13#10'Old Value: ' + Sender.AsString 
                              + #13#10'New Value: ' + Text
                             );
      end;
end;

If you want to avoid persisting your fields you can dynamically assign the Method to the field, e.g. after opening the Dataset.

procedure TForm1.ADataSetAfterOpen(DataSet: TDataSet);
Var
 i:Integer;
begin
  for I := 0 to Dataset.FieldCount - 1 do
      begin
        if Dataset.Fields[i].DataType in [ftFloat, ftCurrency, ftBCD] then
           Dataset.Fields[i].OnSetText := ADataSetAFloatFieldSetText;            
      end;
end;