如何访问 TCRDBGrid 组件中的搜索文本?

发布于 2024-09-28 17:33:54 字数 648 浏览 7 评论 0原文

有没有办法访问在 Devart(核心实验室)TCRDBGrid 搜索栏中输入的文本?

我尝试实现 OnKeyDown 事件,但仅当选择网格本身时,在搜索字段中输入时该事件才发生。没有其他看起来相关的事件。

文本似乎进入 TEdit,它是 TCRGridTitleEdit 的一部分,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。

Is there a way to access the text entered in a Devart (Core Lab) TCRDBGrid search bar?

I tried implementing the OnKeyDown event but the event didn't happen during entry in the search fields, only when the grid itself was selected. There are no other events that look relevant.

The text appears to go into a TEdit which is part of a TCRGridTitleEdit, which is part of TCRDBGrid but is private.

Is there a way to get the search text?

Example TCFGBGrid:

+---------+------------+
| UserId  |  UserName  | <- Column titles
+---------+------------+
|[987654] | [        ] | <- Search bar (searching for UserId 987654)
+---------+------------+
| 123456  |  Wile Cau  | <- Data
+---------+------------+
|   ...   |    ...     | <- More data

If UserId 987654 doesn't exist, I want to prompt something like "Create new user 987654?" and potentially create a new record with UserId set to 987654.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-10-05 17:33:54

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;

TCRDBGrid is distributed with source code. You can modify source code:

declare the event type:

  TOnTextChanged = procedure (Sender: TObject; Text: string) of object;

add event declaration in the TCRGridTitleEdit class:

private 
    FOnFilterChanged: TOnTextChanged;
    FOnSearchChanged: TOnTextChanged;

public
    property OnFilterChanged: TOnTextChanged read FOnFilterChanged write FOnFilterChanged;
    property OnSearchChanged: TOnTextChanged read FOnSearchChanged write FOnSearchChanged;

call this event in the ProcessEdit procedure:

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;

and add event declaration in the TCRDBGrid class:

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;

implementation

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文