有没有办法访问在Devart(核心实验室)TCRDBGrid搜索栏中输入的文本?
我尝试实现OnKeyDown事件,但只有在选择了网格本身时才会在搜索字段中输入时发生事件。没有其他事件看起来相关。
该文本似乎进入了一个TEDit,它是TCRGridTitleEdit的一部分,它是TCRDBGrid的一部分,但是是私有的。
有没有办法获取搜索文本?
示例TCFGBGrid:
+---------+------------+
| UserId | UserName | <- Column titles
+---------+------------+
|[987654] | [ ] | <- Search bar (searching for UserId 987654)
+---------+------------+
| 123456 | Wile Cau | <- Data
+---------+------------+
| ... | ... | <- More data
如果UserId 987654不存在,我想提示“创建新用户987654?”之类的内容。并且可能创建UserId设置为987654的新记录。
答案 0 :(得分:3)
TCRDBGrid随源代码一起发布。您可以修改源代码:
声明事件类型:
TOnTextChanged = procedure (Sender: TObject; Text: string) of object;
在TCRGridTitleEdit类中添加事件声明:
private
FOnFilterChanged: TOnTextChanged;
FOnSearchChanged: TOnTextChanged;
public
property OnFilterChanged: TOnTextChanged read FOnFilterChanged write FOnFilterChanged;
property OnSearchChanged: TOnTextChanged read FOnSearchChanged write FOnSearchChanged;
在ProcessEdit过程中调用此事件:
procedure TCRGridTitleEdit.ProcessEdit;
begin
if (FActiveColumn = nil) or (CRDBGrid = nil) or not FEdit.Modified then
Exit;
if FAsFilter then
with CRDBGrid do begin
try
TCRColumn(FActiveColumn).GetFilterExpression(FEdit.Text);
FFilterExpressions[FActiveColumn.Index] := FEdit.Text;
Self.Caption := FFilterExpressions[FActiveColumn.Index];
if Assigned(FOnFilterChanged) then
FOnFilterChanged(self, FEdit.Text);
except
on EConvertError do begin
FEdit.SelectAll;
raise;
end;
end;
end
else
try
with FActiveColumn.Field do
DataSet.Locate(FieldName, {$IFDEF CLR}Variant{$ENDIF}(FEdit.Text), [loCaseInsensitive,loPartialKey]);
if Assigned(FOnSearchChanged) then
FOnSearchChanged(self, FEdit.Text);
except
on EConvertError do
Exit;
end;
end;
并在TCRDBGrid类中添加事件声明:
private
function GetOnFilterChanged: TOnTextChanged;
procedure SetOnFilterChanged(Value: TOnTextChanged);
function GetOnSearchChanged: TOnTextChanged;
procedure SetOnSearchChanged(Value: TOnTextChanged);
published
property OnFilterChanged: TOnTextChanged read GetOnFilterChanged write SetOnFilterChanged;
property OnSearchChanged: TOnTextChanged read GetOnSearchChanged write SetOnSearchChanged;
实施
function TCRDBGrid.GetOnFilterChanged: TOnTextChanged;
begin
Result := CRGridTitleEdit.OnFilterChanged;
end;
procedure TCRDBGrid.SetOnFilterChanged(Value: TOnTextChanged);
begin
CRGridTitleEdit.OnFilterChanged := Value;
end;
function TCRDBGrid.GetOnSearchChanged: TOnTextChanged;
begin
Result := CRGridTitleEdit.OnSearchChanged;
end;
procedure TCRDBGrid.SetOnSearchChanged(Value: TOnTextChanged);
begin
CRGridTitleEdit.OnSearchChanged := Value;
end;