TDBGrid 中的就地编辑器将通过调用更新其内容
procedure TInplaceEdit.UpdateContents;
begin
Text := '';
EditMask := Grid.GetEditMask(Grid.Col, Grid.Row);
Text := Grid.GetEditText(Grid.Col, Grid.Row);
MaxLength := Grid.GetEditLimit;
end;
GetEditMask 的实现方式如下:
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;
和GetEditLimit 像这样:
function TCustomDBGrid.GetEditLimit: Integer;
begin
Result := 0;
if Assigned(SelectedField) and (SelectedField.DataType in [ftString, ftWideString]) then
Result := SelectedField.Size;
end;
您有多种方法可以达到我认为的所需行为。
方法 1 的代码可能如下所示:
// Opening of dataset
...
DataSet.FieldByName('FloatField').EditMask := '00.00';
这将掩码在小数点分隔符前后需要两位数。有关口罩的更多信息,请参阅TEditMask。
对于方法 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;
就像 kobik 建议的那样,您可以将此类用作插入器类。为此,请在要使用该网格的单位中添加TDBGrid = class(TMyDBGrid);。如果您在要使用的同一单元中声明 TMyDBGrid,请明确类型引用 TMyDBGrid = class(Vcl.DBGrids.TDBGrid)。