從OnDrawColumnCell:
一種OnDrawColumnCell事件處理程序可以調用DefaultDrawColumnCell 方法以指示數據識別網格寫在 單元中的數據值。
使用DefaultDrawColumnCell這樣。這是Delphi代碼,但你可以很容易地轉換它。
procedure TForm1.DBGridDrawColumnCell(Sender: TObject;const Rect: TRect;
DataCol: Integer; Column: TColumnEh;State: TGridDrawState);
begin
.....
DBGrid.Canvas.Brush.Color := clBlue;
DBGrid.DefaultDrawColumnCell(Rect,DataCol,Column,State);
....
更新
如何繪製DBGrid的活動行,無需設置dgRowSelect或dgMultiSelect。
- 我們需要獲取當前行的頂部位置。
定義類的TDBGrid的是inheritant使CellRect,山口和行公衆:
type
TMyDBGrid = class(TDBGrid)
public
function CellRect(ACol, ARow: Longint): TRect;
property Col;
property Row;
end;
function TMyDBGrid.CellRect(ACol, ARow: Longint): TRect;
begin
Result := inherited CellRect(ACol, ARow);
end;
現在,我們可以檢查當前單元格的頂部OnDrawColumnCell事件:
procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var Col,Row : Integer;
begin
col := TMyDbGrid(DBGrid1).Col;
row := TMyDbGrid(DBGrid1).Row;
if (Rect.Top = TMyDBGrid(DBGrid1).CellRect(Col,Row).Top) and
(not (gdFocused in State) or not Focused) then
DBGrid1.Canvas.Brush.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
朋友你好,謝謝你的回答。我已經在使用這種代碼並且已經繪製了行。我需要知道的是如何僅繪製ACTIVE ROW,而不設置dgRowSelect或dgMultiSelect。換句話說,如何確定活動行(注意State.Contains(gdSelected)和grid-> SelectedRows-> CurrentRowSelected在這種情況下不起作用) – sgm
@sgm查看更新 –
沒有聚焦的var! – sgm