如何让DBGrid中的单元格中的文字根据条件显示不同的颜色?(又是给出全部的分。。。) 如题,谢谢各位啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在 Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。示例程序的运行:在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);var i :integer;beginif gdSelected in State then Exit;//定义表头的字体和背景颜色:for i :=0 to (Sender as TDBGrid).Columns.Count-1 dobegin(Sender as TDBGrid).Columns.Title.Font.Name :='宋体'; //字体(Sender as TDBGrid).Columns.Title.Font.Size :=9; //字体大小(Sender as TDBGrid).Columns.Title.Font.Color :=$000000ff; //字体颜色(红色)(Sender as TDBGrid).Columns.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 的边框beginPen.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; 2.Delphi5 - 隔行改变DBGrid网格颜色 : 在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState);var i:integer;beginif gdSelected in State then Exit; //隔行改变网格背景色: if adoQuery1.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 的边框beginPen.Color := $00ff0000; //定义画笔颜色(蓝色)MoveTo(Rect.Left, Rect.Bottom); //画笔定位LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线Pen.Color := clbtnface; //定义画笔颜色(兰色)MoveTo(Rect.Right, Rect.Top); //画笔定位LineTo(Rect.Right, Rect.Bottom); //画绿色end;end; procedure Tfrm.dbgDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);begin if 条件 then DBG.Canvas.Font.Color := clblue; if 条件 then DBG.Canvas.Font.Color := clred; if 条件 then DBG.Canvas.Font.Color := clGradientActiveCaption; DBG.DefaultDrawColumnCell(Rect, DataCol, Column, State);end; http://dev.csdn.net/article/53/53439.shtm DBGrid应用全书(一) http://dev.csdn.net/article/53/53440.shtm DBGrid使用全书(二)http://dev.csdn.net/article/53/53441.shtm DBGrid使用全书(三) http://dev.csdn.net/article/53/53442.shtm DBGrid使用全书(四)http://dev.csdn.net/article/53/53443.shtm DBGrid使用全书(五) 迫切期待高人指点迷津 USB(老之来领分噢) 创建的DLL,怎么调用报错了? 谁有hotimage for d7的版本啊??(救命) delphi能否调用c++编写的类动态库 zebra s400条码打印机,如何编程? 如何实现全屏或者任意大小播放?高手来! EXCEL表导入数据库的问题 这个问题比较特别! 高手请进,如何运行内存映射文件? 关于FASTREPORT的问题 求一sql语句,谢谢
以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。
示例程序的运行:
在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。
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.Title.Font.Name :='宋体'; //字体
(Sender as TDBGrid).Columns.Title.Font.Size :=9; //字体大小
(Sender as TDBGrid).Columns.Title.Font.Color :=$000000ff; //字体颜色(红色)
(Sender as TDBGrid).Columns.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;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i:integer;
begin
if gdSelected in State then Exit; //隔行改变网格背景色:
if adoQuery1.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 := clbtnface; //定义画笔颜色(兰色)
MoveTo(Rect.Right, Rect.Top); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画绿色
end;
end;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if 条件 then
DBG.Canvas.Font.Color := clblue;
if 条件 then
DBG.Canvas.Font.Color := clred; if 条件 then
DBG.Canvas.Font.Color := clGradientActiveCaption;
DBG.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
http://dev.csdn.net/article/53/53439.shtm DBGrid应用全书(一)
http://dev.csdn.net/article/53/53440.shtm DBGrid使用全书(二)
http://dev.csdn.net/article/53/53441.shtm DBGrid使用全书(三)
http://dev.csdn.net/article/53/53442.shtm DBGrid使用全书(四)
http://dev.csdn.net/article/53/53443.shtm DBGrid使用全书(五)