改变DBGRID的颜色?

fee 2000-01-03 03:23:00
如何使DBGRID网格的颜色随此格中的数据值的变化而变化。如<60的网格为红色?
...全文
250 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Venne 2000-01-10
  • 打赏
  • 举报
回复
其实超级大胃已经说得很详细了,我觉得没有再讨论的必要,只要在DRAWDATACELL事件里按自己的意思处理数据就可以实现丰富的色彩。我觉得已经够了。
zyb 2000-01-10
  • 打赏
  • 举报
回复
利用TCANVAS可以实现!
superdavid 2000-01-07
  • 打赏
  • 举报
回复


  Delphi中数据控制构件DBGrid是用来反映数据表的最重要、也是最常用的构件。在应用程序中,如果以彩色的方式来显示DBGrid,将会增加其可视性,尤其在显示一些重要的或者是需要警示的数据时,可以改变这些数据所在的行或列的前景和背景的颜色。

  DBGrid属性DefaultDrawing是用来控制Cell(网格)的绘制。若DefaultDrawing的缺省设置为True,意思是Delphi使用DBGrid的缺省绘制方法来制作网格和其中所包含的数据,数据是按与特定列相连接的Tfield构件的DisplayFormat或EditFormat特性来绘制的;若将DBGrid的DefaultDrawing特性设置成False,Delphi就不绘制网格或其内容,必须自行在TDBGrid的OnDrawDataCell事件中提供自己的绘制例程(自画功能)。

  在这里将用到DBGrid的一个重要属性:画布Canvas,很多构件都有这一属性。Canvas代表了当前被显示DBGrid的表面,你如果把另行定义的显示内容和风格指定给DBGrid对象的Canvas,DBGrid对象会把Canvas属性值在屏幕上显示出来。具体应用时,涉及到Canvas的Brush属性和FillRect方法及TextOut方法。Brush属性规定了DBGrid.Canvas显示的图像、颜色、风格以及访问Windows GDI 对象句柄,FillRect方法使用当前Brush属性填充矩形区域,方法TextOut输出Canvas的文本内容。

  以下用一个例子来详细地说明如何显示彩色的DBGrid。在例子中首先要有一个DBGrid构件,其次有一个用来产生彩色筛选条件的SpinEdit构件,另外还有ColorGrid构件供自由选择数据单元的前景和背景的颜色。

  1.建立名为ColorDBGrid的Project,在其窗体Form1中依次放入所需构件,并设置属性为相应值,具体如下所列:
   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属性的构件中,让应用程序的用户界面更加友好。


--------------------------------------------------------------------------------
kxy 2000-01-03
  • 打赏
  • 举报
回复
同意tide
tide 2000-01-03
  • 打赏
  • 举报
回复
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;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧