程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> delphi 更改DBGrid 顏色技巧

delphi 更改DBGrid 顏色技巧

編輯:Delphi

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


[delphi]
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; 

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、多種改變方式


[delphi]
<SPAN style="COLOR: #000000">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;</SPAN> 

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、縱向斑馬線效果:實現網格的奇數列和偶數列分別以不同的顏色顯示以區別相鄰的數據列。


[delphi]
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); 

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、縱向斑馬線,同時以紅色突出顯示當前單元格效果:以突出顯示當前選中的字段。

[delphi]
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); 

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、在數據網格中以紅色突出顯示當前選中的行。


[delphi]
設置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); 

設置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、行突顯的斑馬線效果:既突出當前行,又區分不同的列(字段)。


[delphi]
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); 

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、橫向斑馬線, 同時以紅色突顯當前行效果。


[delphi]
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); 

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、雙向斑馬線效果:即行間用不同色區分,同時,選中行以縱向斑馬線效果區分不同的列。


[delphi]
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); 

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);


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved