1. 程式人生 > >delphi 更改DBGrid 顏色技巧

delphi 更改DBGrid 顏色技巧

1、根據條件更改某一單元格的顏色

procedure TMainFrm.First_DGDrawColumnCell(Sender: TObject;  
  const Rect: TRect; DataCol: Integer; Column: TColumn;  
  State: TGridDrawState);  
begin  
  if DM.FirstListStatus.AsString='生產' then begin  
     First_DG.Canvas.Font.Color:=clGreen  
   end  
  else begin  
     First_DG.Canvas.Font.Color:
=clRed; end; if DataCol = 4 then First_DG.DefaultDrawColumnCell(Rect,DataCol,Column,State); end;

2、多種改變方式

procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;  
  const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);  
var i :integer;  
begin
if gdSelected in State then Exit; //定義表頭的字型和背景顏色: for i :=0 to (Sender as TDBGrid).Columns.Count-1 do begin (Sender as TDBGrid).Columns[i].Title.Font.Name :='宋體'; //字型 (Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字型大小 (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //
字型顏色(紅色) (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(綠色) 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); //定義背景顏色 //定義網格線的顏色: 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;

3、縱向斑馬線效果:實現網格的奇數列和偶數列分別以不同的顏色顯示以區別相鄰的資料列。

Case DataCol Mod 2 = 0 of  
  True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶數列用藍色  
 False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇數列用淺綠色  
End;  
DbGrid1.Canvas.Pen.Mode:=pmMask;  
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);  

4、縱向斑馬線,同時以紅色突出顯示當前單元格效果:以突出顯示當前選中的欄位。

Case DataCol Mod 2 = 0 of  
True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶數列用藍色  
False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇數列用淺綠色  
End;  
If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then  
If Not DbGrid1.SelectedRows.CurrentRowSelected then  
DbGrid1.Canvas.Brush.Color:=clRed; file://當前選中單元格顯示紅色  
DbGrid1.Canvas.Pen.Mode:=pmMask;  
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);  

5、在資料網格中以紅色突出顯示當前選中的行。

設定DbGrid控制元件的Options屬性中的dgRowSelect屬性為真,Color屬性為clAqua(背景色)  
在DbGrid的DrawColumnCell事件中編寫如下程式碼:  
  
if ((State = [gdSelected]) or (State=[gdSelected gdFocused])) then  
DbGrid1.Canvas.Brush.color:=clRed; file://當前行以紅色顯示,其它行使用背景的淺綠色  
DbGrid1.Canvas.pen.mode:=pmmask;  
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);

6、行突顯的斑馬線效果:既突出當前行,又區分不同的列(欄位)。

if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then  
begin  
Case DataCol Mod 2 = 0 of  
True : DbGrid1.Canvas.Brush.color:=clRed; file://當前選中行的偶數列顯示紅色  
False: DbGrid1.Canvas.Brush.color:=clblue; file://當前選中行的奇數列顯示藍色  
end;  
DbGrid1.Canvas.pen.mode:=pmmask;  
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);  

7、橫向斑馬線, 同時以紅色突顯當前行效果。

Case Table1.RecNo mod 2 = 0 of file://根據資料集的記錄號進行判斷  
True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶數行用淺綠色顯示  
False: DbGrid1.Canvas.Brush.color:=clblue; file://奇數行用藍色表示  
end;  
if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then file://選中行用紅色顯示  
DbGrid1.Canvas.Brush.color:=clRed;  
DbGrid1.Canvas.pen.mode:=pmMask;  
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);  

8、雙向斑馬線效果:即行間用不同色區分,同時,選中行以縱向斑馬線效果區分不同的列。

Case Table1.RecNo mod 2 = 0 of file://根據資料集的記錄號進行判斷  
True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶數行用淺綠色顯示  
False: DbGrid1.Canvas.Brush.color:= clblue; file://奇數行用藍色表示  
end;  
If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then  
Case DataCol mod 2 = 0 of  
True : DbGrid1.Canvas.Brush.color:=clRed; file://當前選中行的偶數列用紅色  
False: DbGrid1.Canvas.Brush.color:= clGreen; file://當前選中行的奇數列用綠色表示  
end;  
DbGrid1.Canvas.pen.mode:=pmMask;  
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);