dbgrid是一个比较复杂的控件,它主要是包括多条记录,下面以dbedit为例:
创建:
constructor TDBEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
inherited ReadOnly := True;
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnActiveChange := ActiveChange;
end;
建立datalink,并赋于不同的事件。
在dbedit的keydown等事件触发后,也会引起datalink状态修改。
内容修改时调用datalink发出通知:
procedure TDBEdit.Change;
begin
FDataLink.Modified;
inherited Change;
end;
别的控件修改后,通过datalink收到消息,并处理,如下:
procedure TDBEdit.DataChange(Sender: TObject);
begin
if FDataLink.Field <> nil then
begin
if FAlignment <> FDataLink.Field.Alignment then
begin
EditText := ''; {forces update}
FAlignment := FDataLink.Field.Alignment;
end;
EditMask := FDataLink.Field.EditMask;
if not (csDesigning in ComponentState) then
begin
if (FDataLink.Field.DataType in [ftString, ftWideString]) and (MaxLength = 0) then
MaxLength := FDataLink.Field.Size;
end;
if FFocused and FDataLink.CanModify then
Text := FDataLink.Field.Text
else
begin
EditText := FDataLink.Field.DisplayText;
if FDataLink.Editing and FDataLink.FModified then
Modified := True;
end;
end else
begin
FAlignment := taLeftJustify;
EditMask := '';
if csDesigning in ComponentState then
EditText := Name else
EditText := '';
end;
end;