---
title: "在Delphi中如何动态更改DBGrid的颜色"
author: "cfeers"
date: "2010-09-21T09:54:54Z"
tags: []
categories: ["编程语言"]
slug: "delphi-dbgrid"
draft: false
lastmod: "2010-09-21T09:54:54Z"
id: 768776
url: "https://cfeers.iteye.com/blog/768776"
---

DBGrid控件是一个有许多用户接口的显示数据库的控件，以下的程序告诉您如何根据显示的内容改变字体的显示颜色。例如，如果一个城市的人口大于200万，我们就让它显示为蓝色。使用的控件事件为DBGrid.OnDrawColumeCell.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:TRect;DataCol:  
Integer; Column: TColumn; State: TGridDrawState);  
begin  
    if Table1.FieldByName('Population').AsInteger &gt; 20000000 then  
      DBGrid1.Canvas.Font.Color := clBlue;  
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);  
end;

    上面的例子是简单的，但是你可以根据自己的需要扩充，例如字体也变化等，甚至你可以调用画圆的函数在数字上画上一个红色的圆圈。