RowSelect打破了OnCellClick的功能,因此我需要关闭RowSelect。 那么,如何通过突出显示当前行的所有单元格来模拟行选择?
答案 0 :(得分: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;
答案 1 :(得分:1)
这对我有用(dgRowSelect=False
和dgMultiSelect=False
):
声明DBGrid
和GridDataLink
的hack类型以访问受保护的方法
和两个变量
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;