cxGrid中如何指定满足条件的某单元格或某些单元格的背景色? ( 积分: 50 )

  • 主题发起人 主题发起人 cnhotel
  • 开始时间 开始时间
C

cnhotel

Unregistered / Unconfirmed
GUEST, unregistred user!
D7+ExpressQuantumGrid 5中的cxGrid控件,在XP下控件自带的Demo无法编译~~

看了它的一个演示例子,tvOrders列的CustomDrawCell事件内容如下:
procedure TCustomDrawTableViewDemoMainForm.tvOrdersCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
ARec: TRect;
ATextToDraw: String;
AIsVertical: Boolean;
val: String;
procedure SetTextToDraw;
begin
ATextToDraw := AViewInfo.GridRecord.DisplayTexts[AViewInfo.Item.Index];
end;
begin
SetTextToDraw;
ARec := AViewInfo.Bounds;
ACanvas.Canvas.Font.Assign(FFonts[Integer(vtDetail), Integer(cdaCell)]);
case FCustomDrawingStyle[Integer(vtDetail), Integer(cdaCell)] of
cdsBkImage:
begin
ACanvas.Brush.Bitmap := nil;
ACanvas.Brush.Assign(FGridBrushDetailCell);
ACanvas.Canvas.FillRect(ARec);
end;
cdsGradient:
begin
AIsVertical := (FColorScheme[Integer(vtDetail), Integer(cdaCell)] = csGrey) or (FColorScheme[Integer(vtDetail), Integer(cdaCell)] = csGold);
DrawGradient(ACanvas.Canvas, ARec, ColorScheme[Integer(FColorScheme[Integer(vtDetail), Integer(cdaCell)]), 1], ColorScheme[Integer(FColorScheme[Integer(vtDetail), Integer(cdaCell)]), 0], 40, AIsVertical)
end;
cdsDependsOnData:
begin
ACanvas.Canvas.Brush.Style := bsSolid;
ACanvas.Canvas.Brush.Color := clBlueLight;

val := VarAsType(AViewInfo.GridRecord.DisplayTexts[tvOrdersPaymentType.Index], varString);
if val = '信用卡' then
ACanvas.Canvas.Font.Color := clRed
else
if val = '牡丹卡' then
ACanvas.Canvas.Font.Color := clBlue
else
if val = '龙卡' then
ACanvas.Canvas.Font.Color := clGreen;
if val = '绿卡' then
ACanvas.Canvas.Font.Color := clFuchsia;
ACanvas.Canvas.FillRect(ARec);
end;
end;

SetBkMode(ACanvas.Canvas.Handle, TRANSPARENT);

ADone := not (FCustomDrawingStyle[Integer(vtDetail), Integer(cdaCell)] in [cdsDefaultDrawing, cdsDependsOnData]);
if ADone then
ACanvas.DrawText(ATextToDraw, AViewInfo.Bounds, 0);
end;

这个例子可以指定满足特定条件的单元格所在行的颜色,但是不能满足仅仅指定特定单元格的背景色,我是菜鸟,希望大家帮指点.DBGrid里面的很多属性这个控件里面都没有~~不知道该如何处理~!~

高手顺便帮介绍下Canvas的含义和用法啊~~
 
D7+ExpressQuantumGrid 5中的cxGrid控件,在XP下控件自带的Demo无法编译~~

看了它的一个演示例子,tvOrders列的CustomDrawCell事件内容如下:
procedure TCustomDrawTableViewDemoMainForm.tvOrdersCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
ARec: TRect;
ATextToDraw: String;
AIsVertical: Boolean;
val: String;
procedure SetTextToDraw;
begin
ATextToDraw := AViewInfo.GridRecord.DisplayTexts[AViewInfo.Item.Index];
end;
begin
SetTextToDraw;
ARec := AViewInfo.Bounds;
ACanvas.Canvas.Font.Assign(FFonts[Integer(vtDetail), Integer(cdaCell)]);
case FCustomDrawingStyle[Integer(vtDetail), Integer(cdaCell)] of
cdsBkImage:
begin
ACanvas.Brush.Bitmap := nil;
ACanvas.Brush.Assign(FGridBrushDetailCell);
ACanvas.Canvas.FillRect(ARec);
end;
cdsGradient:
begin
AIsVertical := (FColorScheme[Integer(vtDetail), Integer(cdaCell)] = csGrey) or (FColorScheme[Integer(vtDetail), Integer(cdaCell)] = csGold);
DrawGradient(ACanvas.Canvas, ARec, ColorScheme[Integer(FColorScheme[Integer(vtDetail), Integer(cdaCell)]), 1], ColorScheme[Integer(FColorScheme[Integer(vtDetail), Integer(cdaCell)]), 0], 40, AIsVertical)
end;
cdsDependsOnData:
begin
ACanvas.Canvas.Brush.Style := bsSolid;
ACanvas.Canvas.Brush.Color := clBlueLight;

val := VarAsType(AViewInfo.GridRecord.DisplayTexts[tvOrdersPaymentType.Index], varString);
if val = '信用卡' then
ACanvas.Canvas.Font.Color := clRed
else
if val = '牡丹卡' then
ACanvas.Canvas.Font.Color := clBlue
else
if val = '龙卡' then
ACanvas.Canvas.Font.Color := clGreen;
if val = '绿卡' then
ACanvas.Canvas.Font.Color := clFuchsia;
ACanvas.Canvas.FillRect(ARec);
end;
end;

SetBkMode(ACanvas.Canvas.Handle, TRANSPARENT);

ADone := not (FCustomDrawingStyle[Integer(vtDetail), Integer(cdaCell)] in [cdsDefaultDrawing, cdsDependsOnData]);
if ADone then
ACanvas.DrawText(ATextToDraw, AViewInfo.Bounds, 0);
end;

这个例子可以指定满足特定条件的单元格所在行的颜色,但是不能满足仅仅指定特定单元格的背景色,我是菜鸟,希望大家帮指点.DBGrid里面的很多属性这个控件里面都没有~~不知道该如何处理~!~

高手顺便帮介绍下Canvas的含义和用法啊~~
 
自己顶上去啊~
 
具体单元格的颜色在cxgrid的具体列的Column1CustomDrawCell里面写
 
楼上说的知道 :)
就是不知道怎么来指定这个单元格的颜色....也就是如何画这个单元格的颜色而不是正行颜色
 
先用ColorDialog1,选择个色,或颜色存在数据库中,取出来一个大概是这样!
procedure TFrmSpSheet.cxGridTreeview1_SPCLCustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if AViewInfo.Item.DataBinding.DefaultCaption = 'SPCOLOR' then
if (AViewInfo.GridRecord.Values[AviewInfo.Item.Index] <> '') and (not
VarIsNull(AViewInfo.GridRecord.Values[AviewInfo.Item.Index])) then
begin
ACanvas.Brush.Color := StringToColor(AViewInfo.GridRecord.Values [AviewInfo.Item.Index]);
ACanvas.Font.Color := ACanvas.Brush.Color;
end;
end;
 
问题已解决:我是这样的:
procedure Trsjh_f.cxGrid1DBTableView1jhts1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
ARec: TRect;
ATextToDraw: String;
val: String;
value_:Integer;
procedure SetTextToDraw;
begin
ATextToDraw := AViewInfo.GridRecord.DisplayTexts[AViewInfo.Item.Index];
end;
function GetPriorValue(FieldName:String;Sorttype:Integer):Integer; //取得上条记录对应值
var GetValue:Integer;
begin
ADOQuery8.SQL.Text:='select '+FieldName+' as value from plan_mx_new where mp_no='''+cxLabel12.Caption+''' and sorttype=:sorttype';
ADOQuery8.Parameters[0].Value:=Sorttype;
ADOQuery8.Open;
if not ADOQuery8.IsEmpty then
begin
GetValue:=ADOQuery8.FieldByName('value').AsInteger;
Result:= GetValue;
end
else Result:=0;
end;
begin
SetTextToDraw;
if (AViewInfo.GridRecord.RecordIndex mod 2=1) and (ATextToDraw<>'') then
begin
value_:=GetPriorValue('jhts1',AViewInfo.GridRecord.RecordIndex);//计划数量
ACanvas.Canvas.Brush.Style := bsSolid;
val := VarAsType(AViewInfo.GridRecord.DisplayTexts[cxGrid1DBTableView1jhts1.Index], varString);
ARec := AViewInfo.Bounds;
if ((StrToInt(val) - value_) > 10) then
begin
ACanvas.Canvas.Brush.Color := clRed; //超出原计划红色显示
ACanvas.Canvas.Font.Color:=clWhite;
end
else
if ((value_ - StrToInt(val)) > 10) then
begin
ACanvas.Canvas.Brush.Color:=clBlack; //未完成状态用黑色表示
ACanvas.Canvas.Font.Color:=clWhite;
end
else
ACanvas.Canvas.Brush.Color:=clGreen;//已完成的用绿色显示
ACanvas.Canvas.FillRect(ARec);
end;
end;
 
结束提问,感谢楼上两位的参与~~
另piggoal大大对cxGrid系列控件研究蛮深的,有空多交流~~
 
后退
顶部