各位大虾:
小弟在编写程序的时候,遇到要在DBGRID中,根据不同的行里面的数据的不同,该行显示出不同的颜色?不知道用什么方法可行,请各位多多指教,帮帮小弟。不胜感激阿!!
小弟在编写程序的时候,遇到要在DBGRID中,根据不同的行里面的数据的不同,该行显示出不同的颜色?不知道用什么方法可行,请各位多多指教,帮帮小弟。不胜感激阿!!
解决方案 »
- 谁能提供一些关于操作OutLook Expreee的资料
- bcp导入文本文件 求助
- SetWindowsHookEx 问题?
- *****关于记录动态调整顺序,高手来一下呀********
- 如何新建一个文件夹,请给出代码。谢谢。
- 我是一个新手,mdi窗口如何加上背景图,各位大虾帮帮忙吧
- 再问adoconnection动态连接数据库的问题,给80分,在线恭候您指教!!
- ADO是免费的吗,ADO哪儿可以下载
- 有关仓库程序的问题
- DEPHI开发基于CORBA的系统的重大安全隐患???请大家讨论
- 我用Indy中的FTP 和 ShellListView 如何获得文件或是目录的图标??
- Interbase数据库程序的移植问题?
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
if not (gdFixed in State) then
begin
with DBGrid1.Canvas do
begin
if DataModule1.Query1.fieldbyname('余额')>0 then
Brush.Color := clGreen
else
Brush.Color:=clred;
FillRect(Rect);
Font.Color:=clblack;
TextOut(Rect.Left, Rect.Top, Field.AsString);
end;
end;
end;end.
Table1.Tablename=’example.db’
Datasource1.Dataset=Table1
ColoredDBGrid1.Datasource=DataSource1
Button1.Caption=’退出’---- 在ColoredDBGrid1的onDRawColoredDBGrid事件中输入下列代码,设定由Wage(工资)来决定在ColoredDBGrid1各行的颜色。
procedure TForm1.ColoredDBGrid1 DRawColoredDBGrid
(Sender: TObject; Field: TField; var Color:
TColor; var Font: TFont);
Var
p : Integer;
begin
p := Table1.FindField('wage').AsInteger;
//取得当前记录的Wage字段的值。
if(p < 500) then begin
//程序将根据wage值设置各行的颜色。
Color := clGreen;
Font.Style := [fsItalic];
//不仅可以改变颜色,还可以改变字体
end;
if(p >= 500) And (p < 800) then
Color := clRed;
if(p >=800) then begin
Color := clMaroon;
Font.Style := [fsBold];
end;
end;
//用‘退出’按钮结束程序运行。
procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
end;
得到预计的结果:第一行和第三行变为红色,第二行变为棕色,第四行为绿色,满足了基本要求。
然后在DBBrid的DrawColumnCell事件中这样做:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Table1.Recno mod 2) <> 0 then begin
DBGrid1.Canvas.Font.Color := clWhite;
DBGrid1.Canvas.Brush.Color := clRed; //这里设置单元格填充色
DBGrid1.Canvas.FillRect(Rect); //这里对单元格进行背景色填充
end;
//输出内容
DBGrid1.Canvas.TextRect( Rect, Rect.Left, Rect.Top, Column.Field.AsString);
end;
在窗体的创建事件中加上代码:
DBGRID.WZebra := true;