1、根据条件更改某一单元格的颜色
- procedure TMainFrm.First_DGDrawColumnCell(Sender: TObject;
- const Rect: TRect; DataCol: Integer; Column: TColumn;
- State: TGridDrawState);
- begin
- if DM.FirstListStatus.AsString='生产' then begin
- First_DG.Canvas.Font.Color:=clGreen
- end
- else begin
- First_DG.Canvas.Font.Color:=clRed;
- end;
- if DataCol = 4 then
- First_DG.DefaultDrawColumnCell(Rect,DataCol,Column,State);
- end;
2、多种改变方式
- <span style="color:#000000;">procedure TMainForm.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[i].Title.Font.Name :='宋体';
- (Sender as TDBGrid).Columns[i].Title.Font.Size :=9;
- (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff;
- (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00;
- end;
-
- if Query1.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
- 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;
- end;</span>
3、纵向斑马线效果:实现网格的奇数列和偶数列分别以不同的颜色显示以区别相邻的数据列。
- Case DataCol Mod 2 = 0 of
- True: DbGrid1.Canvas.Brush.Color:= clBlue; file:
- False: DbGrid1.Canvas.Brush.Color:= clAqua; file:
- End;
- DbGrid1.Canvas.Pen.Mode:=pmMask;
- DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
4、纵向斑马线,同时以红色突出显示当前单元格效果:以突出显示当前选中的字段。
- Case DataCol Mod 2 = 0 of
- True: DbGrid1.Canvas.Brush.Color:= clBlue; file:
- False: DbGrid1.Canvas.Brush.Color:= clAqua; file:
- End;
- If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then
- If Not DbGrid1.SelectedRows.CurrentRowSelected then
- DbGrid1.Canvas.Brush.Color:=clRed; file:
- DbGrid1.Canvas.Pen.Mode:=pmMask;
- DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
5、在数据网格中以红色突出显示当前选中的行。
- 设置DbGrid控件的Options属性中的dgRowSelect属性为真,Color属性为clAqua(背景色)
- 在DbGrid的DrawColumnCell事件中编写如下代码:
-
- if ((State = [gdSelected]) or (State=[gdSelected gdFocused])) then
- DbGrid1.Canvas.Brush.color:=clRed; file:
- DbGrid1.Canvas.pen.
|
请发表评论