如何在DBGrid中突出显示单元格?

3
我正在尝试以编程方式突出显示TDBGrid子类中的当前单元格。如果我执行DBGrid.SetFocus,我会得到下面的组合框箭头,但这对我来说不够突出。更新:我已经执行了DBGrid.SelectedField:= DataSource.FieldByName('Name');为了更吸引用户的注意力,我还设置了:
DBGrid.Columns[x].Title.Font.Style := [fsbold, fsunderline];  

我设置了一个计时器,五秒后执行以下操作:

DBGrid.Columns[x].Title.Font.Style := [];

奇怪的是,时间到后单元格会变成蓝色(如下图所示)。这正是我一开始想要的亮点。但我对网格不够了解,不知道如何直接实现这一点。
我的问题是:如何使网格单元格像下面的蓝色示例一样突出显示?我以前从未做过这样的事情,所以有点迷茫。这是一个InPlaceEditor函数吗?
我正在使用TDBGrid的一个子类,所以我不确定我看到的行为是否固有于TDBGrid,还是仅存在于子类中(在这种情况下,我知道我的问题无法在这里回答)。 Two controls

SelectedField 属性不是你需要的吗? - iMan Biglari
@iManBiglari 是的,我之前已经在做这个了,但是忘记记录下来了。我已经编辑了原始帖子。问题仍然存在。 - RobertFrank
好的。在设置SelectedField之后,调用Invalidate()Repaint()如何? - iMan Biglari
我也对图片上的问题很感兴趣。这两种状态有什么区别? - iMan Biglari
@iManBiglari 添加Invalidate和Repaint都无济于事。我也不知道图像中这两种状态之间的区别!顺便说一句,谢谢你的帮助! - RobertFrank
1个回答

4

我一直在使用以下方法(D2007),使用 DBGrid: OnDrawColumnCell 事件。

procedure TForm1.DBGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin 
  //Make the column blue if the title is bold
  if (fsBold in Column.Title.Font.Style) then
    TDBGrid(Sender).Canvas.Brush.Color := $00fff0e1;

  //Set the selected row to white/bold text on blue background
  if (gdSelected in State) then
    begin
      TDBGrid(Sender).Canvas.Brush.Color := clHighlight;
      TDBGrid(Sender).Canvas.Font.Style := Font.Style + [fsBold];
      TDBGrid(Sender).Canvas.Font.Color := clHighlightText;
    end;

  //Update the grid
  TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

应该可以的,Pieter。谢谢,但我希望以某种方式切换网格单元格到一种状态,导致高亮显示,就像今天发生的某种未知方法一样。 - RobertFrank

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接