【问题标题】:Highlight specific text in DBGrid突出显示 DBGrid 中的特定文本
【发布时间】:2016-08-10 12:40:26
【问题描述】:

我正在执行查询并在 dbgrid 中显示返回的数据。

我想突出显示符合搜索条件的项目。比如:

搜索方式:“测试

在 DBGrid 中,返回的数据是 .

ID     Return
1      This is a **test**
2      **Test**ing

这里的目标毫无疑问是查询数据。但是如何突出显示 DBGrid 中的特定文本?

重要提示:仅应突出显示文本的特定部分。

注意:提供的信息是为了说明清楚,并不完全符合现实。

【问题讨论】:

  • 这在标准 DBGrid 中是不可能的。根据help center,关于另一个组件的问题在这里是题外话。我们不是组件购物网络。
  • 理论上您可以通过将 Default Drawing 设置为 false 并使用 OnDrawColumnCell 和/或 OnDrawDataCell 事件自己绘制单元格,但这是相当低级别的大量工作。网格本身做不到。
  • DevExpress gridview 开箱即用,但并不便宜。
  • 感谢@KenWhite 的回答。但我的目标是解决问题,不一定要购买组件,可以是免费软件或像 ValMarinov 那样的智能答案。无论如何,我删除了文本中偏离主题的部分;)

标签: delphi delphi-xe7


【解决方案1】:

此过程在 DbGrid 中突出显示“FilterText”

procedure HighlightCellText(AGrid :TDbGrid; const ARect : TRect; AColumn : TColumn;  FilterText : string; AState:TGridDrawState ;
  BkColor : TColor = clYellow; SelectedBkColor : TColor = clGray);
var
  HlRect : TRect;
  Position : Integer;
  HlText, FilterColName,DisplayText: string;
  i, offset : Integer;
begin
   DisplayText := Acolumn.Field.AsString;
   Position := Pos(AnsiLowerCase(FilterText), AnsiLowerCase(DisplayText){  AnsiLowerCase(AColumn.DisplayText)});
   if Position > 0 then
   begin
     // set highlight area
     case AColumn.Alignment of
       taLeftJustify:  HlRect.Left := ARect.Left + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1)) + 1;
       taRightJustify: begin
         Offset := AGrid.Canvas.TextWidth(Copy(DisplayText, 1,1)) - 1;
         HlRect.Left :=  (ARect.Right - AGrid.Canvas.TextWidth(DisplayText)-offset) + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1));
       end;
       taCenter: begin
         Offset := ((ARect.Right - ARect.Left) div 2) - (AGrid.Canvas.TextWidth(DisplayText) div 2) - (AGrid.Canvas.TextWidth(Copy(DisplayText, 1,1)) - 2);

         HlRect.Left := (ARect.Right - AGrid.Canvas.TextWidth(DisplayText)- offset) + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1));
       end;
     end;

     HlRect.Top := ARect.Top + 1;
     HlRect.Right := HlRect.Left +AGrid.Canvas.TextWidth(Copy(DisplayText, Position, Length(FilterText))) + 1 ;
     HlRect.Bottom := ARect.Bottom - 1;

     //check for  limit of the cell
     if HlRect.Right > ARect.Right then
       HlRect.Right := ARect.Right;

     // setup the color and draw the rectangle in a width of the matching text
     if gdSelected in AState then
       AGrid.Canvas.Brush.Color := SelectedBkColor
     else
       AGrid.Canvas.Brush.Color := BkColor;

     AGrid.Canvas.FillRect(HlRect);

     HlText := Copy(DisplayText,Position, Length(FilterText));
     AGrid.Canvas.TextRect(HlRect,HlRect.Left + 1,HlRect.Top + 1, HlText);
   end;
end;

在 DbGrid.OnDrawColumnCell 事件中使用它:

例如突出显示文本是“ro”。

procedure TForm6.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
 begin
   HighlightCellText(TDBGrid(Sender),Rect, Column,'ro',State);
end;

结果:

编辑:

A litle demo

【讨论】:

  • 您能否提供一个使用此功能的演示,其中用户在编辑控件中键入搜索短语并且网格突出显示该短语(不滚动网格)?我能看到的唯一方法是使整个网格无效,这会导致一些难看的闪烁。
  • 非常感谢 Val Marinov!给定的方法可以完美运行,没有任何问题。
  • @KenWhite 好的。这是演示。够了吗? :youtube.com/watch?v=pGKSDT5eSPA&feature=youtu.be
  • 是的。 :-) 做得很好。我必须用真实数据(比网格中显示的更多列、更多行)来测试闪烁问题,但它似乎运作良好。谢谢。 :-)
猜你喜欢
  • 2015-07-19
  • 1970-01-01
  • 2013-01-23
  • 2020-12-17
  • 2020-04-17
  • 2023-03-13
  • 2018-02-16
  • 2013-05-28
  • 1970-01-01
相关资源
最近更新 更多