Delphi中DBGrid行列顏色設定

來源:互聯網
上載者:User
1.   縱向斑馬線效果:實現網格的奇數列和偶數列分別以不同的顏色顯示,以區別相鄰的資料列。  
   
    file://在DbGrid的DrawColumnCell事件中編寫如下代碼:  
   
    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);  
   
   
  2.   縱向斑馬線,同時以紅色反白目前的儲存格效果:以反白當前選中的欄位。    
    file://將上述代碼修改為:  
   
    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=[gdSelected,gdFocused]))   then  
   
      If   Not   DbGrid1.SelectedRows.CurrentRowSelected   then  
   
        DbGrid1.Canvas.Brush.Color:=clRed;   file://當前選中儲存格顯示紅色         DbGrid1.Canvas.Pen.Mode:=pmMask;  
   
        DbGrid1.DefaultDrawColumnCell   (Rect,   DataCol,   Column,   State);  
   
  上述兩種方法突出了列的顯示效果。  
   
    3.在資料格中以紅色反白當前選中的行。  
   
    設定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);  
   
    4.行突顯的斑馬線效果:既突出當前行,又區分不同的列(欄位)。  
   
    file://其它屬性設定同3,將上述代碼修改為:  
   
    if   ((State   =   [gdSelected])   or   (State=[gdSelected,gdFocused]))   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);  
   
    end;  
   
   
   
    5.橫向斑馬線,   同時以紅色突顯當前行效果。    
    file://其它屬性設定同3,將上述代碼修改為:  
   
    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=[gdSelected,gdFocused]))   then   file://選中行用紅色顯示  
   
     DbGrid1.Canvas.Brush.color:=clRed;  
   
     DbGrid1.Canvas.pen.mode:=pmMask;  
   
     DbGrid1.DefaultDrawColumnCell   (Rect,   DataCol,   Column,   State);  
   
    6.雙向斑馬線效果:即行間用不同色區分,同時,選中行以縱向斑馬線效果區分不同的列。  
   
    file://其它屬性設定同3,將上述代碼修改為:  
   
    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=[gdSelected,gdFocused]))   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);  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.