I don't know if this helps, but I could color individual cells in a DBGrid . This is what I just tested....
I created a form, dropped a TTable component it, and pointed it to the EMPLOYEE.DB database in the DBDEMOS database. I dropped a Datasource and DBGrid on the form so that it showed on the form.I thought a simple test would be, for the employee number in the EMPLOYEE.DB table, check if it's an odd number. If it's an odd number, then turn that cell green.Then, the only code I attached was to the DBGrid's OnDrawColumnCell event, which looks as follows....--------------------------------------------------------------------------------procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:
TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
holdColor: TColor;
begin
holdColor := DBGrid1.Canvas.Brush.Color; {store the original color}
if Column.FieldName = 'EmpNo' then {only do for the cell displaying
EmpNo}
if (Column.Field.AsInteger mod 2 <> 0) then begin
DBGrid1.Canvas.Brush.Color := clGreen;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
DBGrid1.Canvas.Brush.Color := holdColor;
end;
end;
I created a form, dropped a TTable component it, and pointed it to the EMPLOYEE.DB database in the DBDEMOS database. I dropped a Datasource and DBGrid on the form so that it showed on the form.I thought a simple test would be, for the employee number in the EMPLOYEE.DB table, check if it's an odd number. If it's an odd number, then turn that cell green.Then, the only code I attached was to the DBGrid's OnDrawColumnCell event, which looks as follows....--------------------------------------------------------------------------------procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:
TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
holdColor: TColor;
begin
holdColor := DBGrid1.Canvas.Brush.Color; {store the original color}
if Column.FieldName = 'EmpNo' then {only do for the cell displaying
EmpNo}
if (Column.Field.AsInteger mod 2 <> 0) then begin
DBGrid1.Canvas.Brush.Color := clGreen;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
DBGrid1.Canvas.Brush.Color := holdColor;
end;
end;
解决方案 »
- 急救:Delphi中IdUDPServer局域网内严重丢包的问题
- Form盖住了 输入法提示窗
- 如何用Delphi 制作像瑞星等杀毒软件那样出现在windows的登陆界面的上面?
- 如何获取动态查询出的cxGrid中的Footer合计值?
- 请高手帮忙:插件、向导、异常扑获机制问题
- 請問如何用編程得到xp某個服務的啟動類型是什麼?
- 兄弟们,怎么判断Edit控件中,输入的第一个字符是“中文字符”,还是“英文字符”?
- listview的问题!谁能帮帮我???
- OLE的含义是什么?(我是初学者,不要见笑)
- 很急的一个问题,请大家帮忙
- 哪里可以找到Install Shiled for delphi4的中文版?
- 请问哪里可下载Free的ASP控件及Free的 Email控件(刚才打错了)
Table1 DatabaseName: DBDEMOS
TableName: EMPLOYEE.DB
Active: True;
DataSource1 DataSet: Table1
DBGrid1 DataSource1: DataSource1
DefaultDrawing: False
SpinEdit1 Increment:200
Value: 20000
ColorGrid1 GridOrdering: go16*1 2.为DBGrid1构件OnDrawDataCell事件编写响应程序:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState);
begin
if Table1.Fieldbyname(′Salary′).value<=SpinEdit1.value then
DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor
else
DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);
end; 这个过程的作用是当SpinEdit1给定的条件得以满足时,如′salary′变量低于或等于SpinEdit1.Value时,DBGrid1记录以ColorGrid1的前景颜色来显示,否则以ColorGrid1的背景颜色来显示。然后调用DBGrid的Canvas的填充过程FillRect和文本输出过程重新绘制DBGrid的画面。 3.为SpinEdit1构件的OnChange事件编写响应代码:
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
DBGrid1.refresh;
end; 当SpinEdit1构件的值有所改变时,重新刷新DBGrid1。 4.为ColorGrid1的OnChange事件编写响应代码:
procedure TForm1.ColorGrid1Change(Sender: TObject);
begin
DBGrid1.refresh;
end; 当ColorGrid1的值有所改变时,即鼠标的右键或左键单击ColorGrid1重新刷新DBGrid1。 5.为Form1窗体(主窗体)的OnCreate事件编写响应代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
ColorGrid1.ForeGroundIndex:=9;
ColorGrid1.BackGroundIndex:=15;
end; 在主窗创建时,将ColorGrid1的初值设定前景为灰色,背景为白色,也即DBGrid的字体颜色为灰色,背景颜色为白色。 6.现在,可以对ColorDBGrid程序进行编译和运行了。当用鼠标的左键或右键单击ColorGrid1时,DBGrid的字体和背景颜色将随之变化。 在本文中,只是简单展示了以彩色方式显示DBGrid的原理,当然,还可以增加程序的复杂性,使其实用化。同样道理,也可以将这个方法扩展到其他拥有Canvas属性的构件中,让应用程序的用户界面更加友好。
--------------------------------------------------------------------------------