運行效果:選中行變為藍色
步驟1:設置dbgrid的options的dgrowselect為true.
步驟2:在dbgrid的ondrawcolumncell事件裡面寫上:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdSelected in state then
dbgrid1.Canvas.Brush.Color:=clblue;
dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
//定義網格線的顏色:
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
with (Sender as TDBGrid).Canvas do //畫 cell 的邊框
begin
Pen.Color := $00ff0000; //定義畫筆顏色(藍色)
MoveTo(Rect.Left, Rect.Bottom); //畫筆定位
LineTo(Rect.Right, Rect.Bottom); //畫藍色的橫線
Pen.Color := $0000ff00; //定義畫筆顏色(綠色)
MoveTo(Rect.Right, Rect.Top); //畫筆定位
LineTo(Rect.Right, Rect.Bottom); //畫綠色的豎線
end;
end;
//隔行改變網格背景色:
if Query1.RecNo mod 2 = 0 then
(Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定義背景顏色
else
(Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定義背景顏色