看了好多的帖子,都是写的设置某行或列的颜色,并不是设置的具体某一格的颜色的。比如,当字段price的值满足某条件时,price所在的单元格字体或是背景设为其他颜色,而不是其所在的行或列都设为此颜色
解决方案 »
- 如何设置Delphi7调用WebServices超时时间
- 三层中客户端为何不能返回标识字段的值
- dll里面显示的模式窗口关闭以后的问题!
- 一个问题很急!第一次遇到!!!我觉着很怪
- 关于delphi7的XPColorMap的使用方法?
- delphi书了.??
- delphi调用webService时报错:ESOAPHTTPException(如何解决)
- 谁可以告诉我怎样做出嗅探器程序呢?
- Delphi如何得到网页中的链接 , 又如何得到网页中的 任意一句<a></>之间的非链接文字?
- 愿天下有共同爱好Delphi编程的朋友与愚兄共讨编程感受.
- combobox的问题??急 用呀??
- 轻松一下,来看看这道爱因斯坦的测试题,他说世界上有98%的人回答不出这道题,看看你是否属于另外的2%。
Field: TField; State: TGridDrawState);
begin
if gdselected in state then
begin
dbgrid1.Canvas.Brush.Color:=clred; //选中的单元格的颜色
dbgrid1.Canvas.FillRect(rect);
dbgrid1.Canvas.Pen.Color:=clwhite; //选中的单元格的字体颜色
if VarType(Field.Value)=varNull then
dbgrid1.Canvas.TextOut(rect.left,rect.top,'')
else
dbgrid1.Canvas.TextOut(rect.left,rect.top,Field.Value);
end;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with DBGrid1 do
begin
if gdSelected in State then
begin
Canvas.Brush.Color := $00C08080;
Canvas.Font.Color := clWhite;
end;
DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
if dgEditing in DBGrid1.Options then
begin
if not (dgRowSelect in DBGrid1.Options) then
DBGrid1.Options := DBGrid1.Options + [dgRowSelect] - [dgEditing] ;
end
else
begin
if dgRowSelect in DBGrid1.Options then
begin
DBGrid1.Options := DBGrid1.Options - [dgRowSelect] + [dgEditing] + [dgAlwaysShowEditor];
DBGrid1.Options := DBGrid1.Options - [dgRowSelect] + [dgEditing] - [dgAlwaysShowEditor]
end
end
end;
OnDrawColumnCell和OnDrawDataCell事件即可。
你的代码是选择时的颜色,我要的是正常情况下的颜色,如
----------------------
ID NAME SEX AGE
----------------------
1 John F 20
2 Rose M 19
3 Jack F 22
----------------------
当SEX为M时显示的NAME为红色,这时Rose应为红色,而其他为默认色,请再看看
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if table1.FieldByName('size').asfloat>=30 then
dbgrid1.Canvas.Font.Color:=clblue;
dbgrid1.DefaultDrawDataCell(rect,column.field,state);
end;//当某条件时,字体颜色突出显示
Delphi中的数据网格控件(TDbGrid)对于显示和编辑数据库中大量的数据起着十分重要的作用;然而,在使用数据网格控件的同时,也往往因为表格中大量的数据不易区分,而令操作者眼花缭乱。如何提高网格控件的易用性,克服它的此项不足呢?本文从改变数据网格的色彩配置角度,提出了一种解决办法。
以下为数据网格控件的6种特殊效果的实现方法,至于数据网格控件与数据集如何连接的方法从略。
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);
7. 随意控制DBGrid 每一行的颜色:
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.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (Field.FieldName='NO') and (Field.DataSet.FieldByName('NAME').AsString='John') then
begin
dbGrid1.Canvas.Brush.Color:=clRed;
dbgrid1.Canvas.Font.Color:=clblue;
end;
dbgrid1.DefaultDrawDataCell(rect,field,state);
end;