how can i colour whole row in DBGrid with rowselect turned off?

谁说我不能喝 提交于 2019-12-02 12:08:18

问题


RowSelect breaks the functionality of OnCellClick, so I need to turn RowSelect off. So then how can I simulate to look of row select by highlighting the all the cell of the current row?


回答1:


This worked for me ( dgRowSelect=False and dgMultiSelect=False ):
Declare hack types for DBGrid and GridDataLink to access protected methods and two variables

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

In OnFormCreate assign the variables to have them available at drawing time:

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

and test selected row using 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;



回答2:


Use the TDBGrid.OnDrawColumnCell event, and set the State to indicate the row is selected.

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;


来源:https://stackoverflow.com/questions/13824573/how-can-i-colour-whole-row-in-dbgrid-with-rowselect-turned-off

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!