精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>数据库技术>>动态使用dbgrid颜色的问题

主题:动态使用dbgrid颜色的问题
发信人: teleme(PassWord)
整理人: teleme(2001-05-26 12:34:04), 站内信件
我记得我曾经发上来过,后来没找到在哪里,重新发一篇吧。
//*******************一下保存为unit1.pas
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, ExtCtrls, Db, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('size').AsInteger >=40 then
  begin
  DBGrid1.Canvas.Font.Color := clRed;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
if (Table1.FieldByName('size').AsInteger >=30) and (Table1.FieldByName('size').AsInteger<40) then
begin
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if (Table1.FieldByName('size').AsInteger >=20) and (Table1.FieldByName('size').AsInteger<30) then
begin
DBGrid1.Canvas.Font.Color := clYellow;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
if (Table1.FieldByName('size').AsInteger >=10) and (Table1.FieldByName('size').AsInteger<20) then
begin
DBGrid1.Canvas.Font.Color := clLime;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;

end.
//******************************* 一下保存为unit1.dfm
object Form1: TForm1
Left = 192
Top = 107
Width = 486
Height = 279
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 478
Height = 252
Align = alClient
Caption = 'Panel1'
TabOrder = 0
object DBGrid1: TDBGrid
Left = 16
Top = 64
Width = 449
Height = 161
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
OnDrawColumnCell = DBGrid1DrawColumnCell
end
end
object Table1: TTable
Active = True
DatabaseName = 'BCDEMOS'
TableName = 'animals.dbf'
Left = 32
Top = 24
end
object DataSource1: TDataSource
DataSet = Table1
Left = 80
Top = 16
end
end
//****************************
工程文件就随便建一个了。
以上为可以使用的,使用时,自己稍稍改动。

[关闭][返回]