如何快速设置DBGrid中每一个单元格的颜色?要快速的啊!(50分)

  • 主题发起人 主题发起人 lingb
  • 开始时间 开始时间
L

lingb

Unregistered / Unconfirmed
GUEST, unregistred user!
如何快速设置DBGrid中每一个单元格的字体颜色?不是整行、整列改变而是改变具体某一单元格的字体颜色?实现的效果是每个单元格颜色不同。
 
我的笔记,供参考:
http://www.delphibbs.com/keylife/iblog_show.asp?xid=4652
 
自己用Canvas绘制
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ... then
begin

end;
end;
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
if DBGrid1.DataSource.DataSet.RecNo mod 2 = 0 then //条件判断
begin
with DBGrid1do
begin
Canvas.Font.Color := clBlack;
Canvas.Brush.Color := $00B1CEEC;
end;
end
else
begin
with DBGrid1do
begin
Canvas.Font.Color := clBlack;
Canvas.Brush.Color := clwhite;
end;
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
 
procedure TFIdleSetup.DBGE_ListDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
column.Color:=clwindow;
column.Font.Color:=clblack;


colName := Column.FieldName;
if colName = 'Test1' then
begin

isSelect := DBGE_list.DataSource.DataSet.FieldByName(colName).AsBoolean;
if isSelect then
begin
DBGE_list.Canvas.Brush.Color:=clSkyBlue;
DBGE_list.DefaultDrawColumnCell(rect,datacol,column,state);
end;

end;
end;
 
sorry ,没细看题目!!!!!!
 
以下的程序告诉您如何根据显示的内容改变字体的显示颜色。例如,如果一个城市的人口大于200万,我们就让它显示为蓝色。使用的控件事件为DBGrid.OnDrawColumeCell.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('Population').AsInteger > 20000000 then begin
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.Canvas.Brush.Color := clYellow;
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
 
我不是要选中时候改变颜色,而是设置每一个单元格颜色,这样一来整个DBGrid填色的时间很长,能否有快速的方法呢?
 
用的是StringGrid
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
NewColor:TColor;
begin
Randomize();
NewColor:=RGB(Random(255),Random(255),Random(255));
self.StringGrid1.Canvas.Brush.Color:=NewColor;
self.StringGrid1.Canvas.FillRect(Rect);
self.StringGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,self.StringGrid1.Cells[ACol,ARow]);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i,j:Integer;
begin
for i:=0 to self.StringGrid1.ColCount-1 do
begin
for j:=0 to self.StringGrid1.RowCount-1 do
begin
self.StringGrid1.Cells[i,j]:= IntToStr(i*j);
end;
end;
end;
 
单击每个格子颜色都在变化!
 
多人接受答案了。
 
后退
顶部