如何使用dgRowSelect = False跟踪所选行

sra*_*ner 1 delphi tdbgrid

当dgRowSelect = False时,如何检测OnDrawColumnCell方法中的选定行?

不是选定的单元格,而是包含所选单元格的行.

Ken*_*ite 8

下面的代码似乎有效.在TDBGrid仍保持SelectedRows更新(即使它不与他们没有吸取dgRowSelect启用),所以你仍然可以访问它们在你的绘制代码.(dgMultiSelect即使不需要,您仍然需要启用dgRowSelect.)

代码让网格完成所有绘图,只需设置Canvas.Brush.Color选定的行.如果单元格的状态恰好是单个单元格的绘图代码,则提供的颜色将被覆盖gdSelected.

我已经将所选行的颜色设置为clFuchsia,并且为了清晰起见,只将选定的单元格保留为默认颜色(网格对于选定的行来说很难看clFuchsia,但它可以用来演示):

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer;
  Column: TColumn; State: TGridDrawState);
var
  Selected: Boolean;
  Grid: TDBGrid;
begin
  Grid := TDBGrid(Sender); 
  if not (gdSelected in State) then
  begin
    Selected := Grid.SelectedRows.CurrentRowSelected;
    if Selected then
      Grid.Canvas.Brush.Color := clFuchsia;
  end;
  Grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
Run Code Online (Sandbox Code Playgroud)

上面的示例结果,选择了第一行和第三行:

在此输入图像描述

当然,您可以使用通常选择的颜色clHighLight; 但我发现它很混乱,因为未选中行的当前单元格与所选行的颜色完全匹配.如果他们直接相邻,那在视觉上很烦人.

  • @Ken,谢谢你选择那个紫红色!你的截图真的从几乎睡眠的状态中唤醒了我:-) [+ 1ed] (5认同)