Delphi – Limit the maximum text length of the Inplace editor in TDBGRID

How to limit the maximum text length of inplace editor in TDBGrid? (Delphi Berlin)

The data type is Float.

TDBGrid The inplace editor in will 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;< br />end;

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 like this:

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

There, you have multiple ways to achieve the behavior I want.

>To limit The field uses the TField EditMask property. This will be returned by the Grid.GetEditMask call. No need to inherit from TDBGrid and overwrite anything. You can control the behavior on a field-by-field basis.
>Create your own TDBGrid descendants ,Override GetEditLimit
According to SelectedField to return MaxLength for the inplace editor

The code of method 1 may look like this:

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

This will mask the need for two digits before and after the decimal separator. About the mask For more information, please see TEditMask

Method 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;

Just like As suggested by kobik, you can use this class as an insert class. For this, add TDBGrid = class(TMyDBGrid); in the cell where you want to use the grid. If you declare TMyDBGrid in the same cell where you want to use it, Please make the type reference clear TMyDBG rid = class(Vcl.DBGrids.TDBGrid).

How to limit the maximum text length of the inplace editor in TDBGrid? (Delphi Berlin)

The data type is Float.

The inplace editor in TDBGrid will update its content by calling< p>

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

The implementation of GetEditMask is as follows:

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

and GetEditLimit like this:

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

There, you have multiple ways to Achieve the behavior I want.

>Use the TField EditMask attribute for the field to be restricted. This will be returned by the Grid.GetEditMask call. No need from TDBGrid inherits and overrides anything. You can control the behavior on a field-by-field basis.
>Create your own TDBGrid descendants and override GetEditLimit
Return MaxLength for the inplace editor based on SelectedField

Method 1 The code might look like this:

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

This will mask the need for two digits before and after the decimal separator. For more information about the mask, see TEditMask

Method 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;

As kobik suggested, you can use this class as an insert class. For this, add TDBGrid = class(TMyDBGrid ); in the cell where you want to use the grid. If you declare TMyDBGrid in the same cell where you want to use it, please make the type reference clear TMyDBGrid = class(Vcl.DBGrids.TDBGrid).

Leave a Comment

Your email address will not be published.