下面请跟着图老师小编一起来了解下关于DBGrid的分类颜色显示,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!
【 tulaoshi.com - 编程语言 】
这两天在csdn里转转,发现很多人问关于DBGRID或DBGRIDEH的分类显示的问题,今天闲下来,就这个问题聊两句。实际上对于DBGRID的自画,delphi 提供了两个事件供我们使用,interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
Query1: TQuery;
DBGrid1: TDBGrid;
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure Query1AfterOpen(DataSet: TDataSet);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
clf:array of Tcolor;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Query1.Active := False;
Query1.DatabaseName := 'DBDEMOS';
Query1.SQL.Add('select * from orders');
DataSource1.DataSet := Query1;
Dbgrid1.DataSource := DataSource1;
Dbgrid1.Align := alClient;
Query1.Active := True;
end;
procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var
i,aa:integer;
c:Tcolor;
begin
query1.DisableControls;
setlength(clf,query1.RecordCount);
query1.First;
i := Low(clf);
clf[i] := clRed;
c := clRed;
aa:=1;
while not query1.Eof do
begin
if (query1.RecNo - aa)9 then
begin
aa := aa + 10;
if c = clRed then c:= clGreen
else c := clRed;
end;
clf[i] := c;
Inc(i);
query1.Next;
end;
query1.First;
query1.EnableControls;
end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
DBGrid1.Canvas.Brush.Color := clf[Dbgrid1.DataSource.DataSet.RecNo-1+Low(clf)];
DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
end.
来源:http://www.tulaoshi.com/n/20160219/1611992.html
看过《关于DBGrid的分类颜色显示》的人还看了以下文章 更多>>