大家好,能否幫忙實現修改DBGrid指定單元格字體的顔色,Thanks(100分)

  • 主题发起人 主题发起人 aikede
  • 开始时间 开始时间
A

aikede

Unregistered / Unconfirmed
GUEST, unregistred user!
顯示在DBGrid中的數據,但單元格的值滿足一定條件時,修改單元字體顔色,請問如何實現,謝謝
 
需要从表格开始遍历到结尾,判断设定的条件,满足的话设置column.Color为列颜色,每次想要通过条件改变颜色都需要遍历表格
 
在DBGrid的如下事件中寫,每段都是一個功能
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i: Integer;
begin

if gdSelected in State then Exit;
//隅砱桶芛腔趼极睿掖劓晇伎ㄩ
for i := 0 to (Sender as TDBGrid).Columns.Count - 1 do
begin
(Sender as TDBGrid).Columns.Title.Font.Name := '冼极'; //趼极
(Sender as TDBGrid).Columns.Title.Font.Size := 9; //趼极湮苤
(Sender as TDBGrid).Columns.Title.Font.Color := clmaroon; //趼极晇伎(綻伎)
(Sender as TDBGrid).Columns.Title.Color := clmoneygreen; //掖劓伎(蟯伎)
end;

//路俴蜊曹厙跡掖劓伎ㄩ

if Table1.RecNo mod 2 = 0 then
(Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //隅砱掖劓晇伎
else
(Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //隅砱掖劓晇伎

//隅砱厙跡盄腔晇伎ㄩ
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);

with (Sender as TDBGrid).Canvas do //賒 cell 腔晚遺
begin
Pen.Color := $00FF0000; //隅砱賒捩晇伎(懦伎)
MoveTo(Rect.Left, Rect.Bottom); //賒捩隅弇
LineTo(Rect.Right, Rect.Bottom); //賒懦伎腔筵盄
Pen.Color := $0000FF00; //隅砱賒捩晇伎(蟯伎)
MoveTo(Rect.Right, Rect.Top); //賒捩隅弇
LineTo(Rect.Right, Rect.Bottom); //賒蟯伎腔旳盄
end;


//載擂囀蜊曹跪俴趼极腔晇伎
if length(Table1.Fields[3].AsString) > 6 then
DBGrid1.Canvas.Font.Color := clBlue
else if length(Table1.Fields[3].AsString) = 0 then
DBGrid1.Canvas.Font.Color := clred ;

DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);


//妗珋厙跡腔杅蹈睿髒杅蹈煦梗眕祥肮腔晇伎珆尨眕梗眈邁腔杅擂蹈﹝
Case DataCol Mod 2 = 0 of
True: DbGrid1.Canvas.Brush.Color:= clWindow; //file://髒杅蹈蚚懦伎
False: DbGrid1.Canvas.Brush.Color:= clinfobk; //file://杅蹈蚚蟯伎
End;
DbGrid1.Canvas.Pen.Mode:=pmMask;
DbGrid1.DefaultDrawColumnCell (Rect,DataCol,Column,State);


end;
 
在DBGrid的OnDrawColumnCell事件里做判断,满足条件则改变画板颜色。例:

procedure TDm_MainForm.DBGrid_CellDataViewDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var
TmpStr: string;
TmpRect: TRect;
TmpGrid: TDBGrid;
TmpStatus: Integer;
TmpDataSet: TDataSet;
begin
TmpDataSet := (Sender as TDBGrid).DataSource.DataSet;
TmpGrid := Sender as TDBGrid;
TmpGrid.Canvas.FillRect(Rect);
TmpRect := Rect;
TmpStr := '';

case DataCol of
2: {'tdd_status'}
begin
TmpStatus := TmpDataSet.FieldbyName('tdd_status').AsInteger;
case TmpStatus of
0, 1, 2, 3, 4:
begin
TmpStr := STCellStates[TmpStatus];
TmpGrid.Canvas.Font.Color := Cc_Dm_StatesColor[TmpStatus];
end;
8, $80..$8F:
TmpStr := SCellState_Pause;
else
TmpStr := SCellState_Error;
end;
end;
else
TmpGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
exit;
end;

DrawText(TmpGrid.Canvas.Handle,
pChar(TmpStr),
Length(TmpStr),
TmpRect, // 包含文字的矩形
DT_CENTER or // 水平居中
DT_SINGLELINE or // 不折行
DT_VCENTER); // 垂直居中
end;
 
謝謝大家給的代碼,但是好像還是有問題,請問怎麽才可是單元格顔色和字體根據值而改變
 
具体说说什么值用什么颜色
 
比如说我要案将字段num的值大于100的单元格背景用红色,字体用白色,代码如何呢

谢谢 
 
//处理DBGrid的OnDrawColumnCell事件,此事件在绘制DBGrid时发生
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName = 'num' then //如果是指定字段
if Column.Field.AsInteger > 100 then //如果字段值大于100
begin
DBGrid1.Canvas.Brush.Color := clRed; //背景色
DBGrid1.Canvas.Font.Color := clWhite; //字体色
DBGrid1.Canvas.FillRect(Rect); //填充背景
DBGrid1.Canvas.TextRect(Rect, 2, 2, Column.Field.AsString); //写字
{ 上面这行可以用DrawText函数,可以方便设置居中等格式 }
end;
end;
 
后退
顶部