Limit maximum text length of inplace editor in TDBGrid

How do I limit the maximum text length of an inplace editor in TDBGrid

? (Delphi Berlin)

Data type - Float.

+3


source to share


1 answer


The inplace editor will TDBGrid

update its content by calling

procedure TInplaceEdit.UpdateContents;
begin
  Text := '';
  EditMask := Grid.GetEditMask(Grid.Col, Grid.Row);
  Text := Grid.GetEditText(Grid.Col, Grid.Row);
  MaxLength := Grid.GetEditLimit;
end;

      

Where GetEditMask

is implemented as follows:

function TCustomDBGrid.GetEditMask(ACol, ARow: Longint): string;
begin
  Result := '';
  if FDatalink.Active then
  with Columns[RawToDataColumn(ACol)] do
    if Assigned(Field) then
      Result := Field.EditMask;
end;

      

and GetEditLimit

as follows:

function TCustomDBGrid.GetEditLimit: Integer;
begin
  Result := 0;
  if Assigned(SelectedField) and (SelectedField.DataType in [ftString, ftWideString]) then
    Result := SelectedField.Size;
end;

      

You have several ways to achieve the desired behavior I think.



  • Use the property TField

    EditMask

    for the field you want to constrain. This will be returned by the call to Grid.GetEditMask. There is no need to inherit from TDBGrid and override anything. Behavior can be controlled individually.

  • Create your own child TDBGrid

    where you override GetEditLimit

    to return MaxLength for inplace editor depending onSelectedField

The code for Approach 1 might look like this:

// Opening of dataset
...
DataSet.FieldByName('FloatField').EditMask := '00.00';

      

This mask will require two digits before and after the decimal point. For more information on masks, see TEditMask

.

For Approach 2:

uses
  Data.DB,
  Vcl.DBGrids;

type
  TMyDBGrid = class(TDBGrid)
  protected
    function  GetEditLimit: Integer; override;
  end;

implementation

{ TMyDBGrid }

function TMyDBGrid.GetEditLimit: Integer;
begin
  Result := inherited GetEditLimit;
  if (Result = 0) and Assigned(SelectedField) and (SelectedField.DataType = ftFloat) then
    Result := 5; // Whatever you decide
end;

      

Like kobik, you can use this class as an interpolator class. To do this, add TDBGrid = class(TMyDBGrid);

this mesh to the device you want to use. If you declared TMyDBGrid

in the same block that you want to use, make a reference like TMyDBGrid = class(Vcl.DBGrids.TDBGrid)

.

+7


source







All Articles