1

RowSelect 破坏了 OnCellClick 的功能,所以我需要关闭 RowSelect。那么如何通过突出显示当前行的所有单元格来模拟行选择的外观呢?

4

2 回答 2

1

使用TDBGrid.OnDrawColumnCell事件,并设置State以指示该行被选中。

procedure TfrmPrimaryCare.dbGrdPCClaimsDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  NewState: TGridDrawState;
  RowSelected: Boolean;
begin
  NewState := State;
  RowSelected := (Sender as TDBGrid).SelectedRows.CurrentRowSelected;
  if (RowSelected) then
    NewState := NewState + [gdSelected];
  TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, NewState);
end;
于 2012-12-11T16:52:34.607 回答
1

这对我有用(dgRowSelect=FalsedgMultiSelect=False):为和访问受保护的方法和两个变量
声明黑客类型DBGridGridDataLink

type
  THackGrid = class(TDBGrid);
  THackDataLink = class(TGridDataLink);
var
  HackGrid: THackGrid;
  HackDataLink: THackDataLink;

OnFormCreate分配变量以使它们在绘图时可用:

procedure TMyForm.FormCreate(Sender: TObject);
begin
  HackGrid := THackGrid(MainGrid);
  HackDataLink := THackDataLink(HackGrid.DataLink);
end;

并使用以下方法测试选定的行TGridDataLink.GetActiveRecord

procedure TMyForm.MainGridDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  if Not (gdSelected in State) and (HackGrid.Row = HackDataLink.GetActiveRecord + 1) then
    MainGrid.Canvas.Brush.Color := clInfoBk;

  MainGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
于 2016-07-20T17:03:28.063 回答