StringGrid的没有, DBGrid的有一个例子, 应该差不多吧 下面的代码可以改变任意单元格的字体、背景颜色procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (DataCol = 1)and(ADOTable1.FieldByName('UserName').Value = 'Administrator') then begin DBGrid1.Canvas.Font.Color := clRed; DBGrid1.Canvas.Brush.Color := clLime; end; DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end;以上代码仅把第一列(从零列开始),第UserName='Administgrator'行的单元格的字体颜色改为红色,背景颜色改为亮绿色
我的意思是说怎么stringgrid的相邻的两个格变颜色 我现在的问题是怎么做循环
if odd(StringGrid.Row) then //奇数行 ... else //偶数行 ...
if odd(stringgrid1.Row) then begin if odd(stringgrid1.Col) then begin stringgrid1.Canvas.Brush.Color := clinfobk; stringgrid1.Canvas.FillRect(rect); end else stringgrid1.Canvas.Brush.Color := clGradientActiveCaption; stringgrid1.Canvas.FillRect(rect); end else stringgrid1.Canvas.Brush.Color := clGradientActiveCaption; stringgrid1.Canvas.FillRect(rect); end
if odd(stringgrid1.Row) then begin if odd(stringgrid1.Col) then begin stringgrid1.Canvas.Brush.Color := clinfobk; stringgrid1.Canvas.FillRect(rect); end else stringgrid1.Canvas.Brush.Color := clGradientActiveCaption; stringgrid1.Canvas.FillRect(rect); end else stringgrid1.Canvas.Brush.Color := clGradientActiveCaption; stringgrid1.Canvas.FillRect(rect); end 请帮忙分析一下哪里写的不对!!!!!!
只取一种就够了 procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var i,j:integer; begin // 相邻行列都变色 {if (ACol mod 2 =ARow mod 2) then TStringGrid(Sender).Canvas.Brush.Color:=clLime else TStringGrid(Sender).Canvas.Brush.Color:=clCream; // 相邻列变色 if (ACol mod 2 =0) then TStringGrid(Sender).Canvas.Brush.Color:=clLime else TStringGrid(Sender).Canvas.Brush.Color:=clCream; } // 相邻列变色 if (ARow mod 2=0) then // TStringGrid(Sender).Canvas.Brush.Color:=clLime else TStringGrid(Sender).Canvas.Brush.Color:=clCream; TStringGrid(Sender).Canvas.Rectangle(Rect); TStringGrid(Sender).Canvas.TextRect(Rect,Rect.Left+4,Rect.Top+4,TStringGrid(Sender).Cells[ACol, ARow]);end;
下面的代码可以改变任意单元格的字体、背景颜色procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (DataCol = 1)and(ADOTable1.FieldByName('UserName').Value = 'Administrator') then
begin
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.Canvas.Brush.Color := clLime;
end;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;以上代码仅把第一列(从零列开始),第UserName='Administgrator'行的单元格的字体颜色改为红色,背景颜色改为亮绿色
我现在的问题是怎么做循环
...
else //偶数行
...
if odd(stringgrid1.Row) then
begin
if odd(stringgrid1.Col) then
begin
stringgrid1.Canvas.Brush.Color := clinfobk;
stringgrid1.Canvas.FillRect(rect);
end
else
stringgrid1.Canvas.Brush.Color := clGradientActiveCaption;
stringgrid1.Canvas.FillRect(rect);
end
else
stringgrid1.Canvas.Brush.Color := clGradientActiveCaption;
stringgrid1.Canvas.FillRect(rect);
end
begin
if odd(stringgrid1.Col) then
begin
stringgrid1.Canvas.Brush.Color := clinfobk;
stringgrid1.Canvas.FillRect(rect);
end
else
stringgrid1.Canvas.Brush.Color := clGradientActiveCaption;
stringgrid1.Canvas.FillRect(rect);
end
else
stringgrid1.Canvas.Brush.Color := clGradientActiveCaption;
stringgrid1.Canvas.FillRect(rect);
end
请帮忙分析一下哪里写的不对!!!!!!
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
i,j:integer;
begin
// 相邻行列都变色
{if (ACol mod 2 =ARow mod 2) then
TStringGrid(Sender).Canvas.Brush.Color:=clLime
else
TStringGrid(Sender).Canvas.Brush.Color:=clCream;
// 相邻列变色
if (ACol mod 2 =0) then
TStringGrid(Sender).Canvas.Brush.Color:=clLime
else
TStringGrid(Sender).Canvas.Brush.Color:=clCream; }
// 相邻列变色
if (ARow mod 2=0) then //
TStringGrid(Sender).Canvas.Brush.Color:=clLime
else
TStringGrid(Sender).Canvas.Brush.Color:=clCream;
TStringGrid(Sender).Canvas.Rectangle(Rect);
TStringGrid(Sender).Canvas.TextRect(Rect,Rect.Left+4,Rect.Top+4,TStringGrid(Sender).Cells[ACol, ARow]);end;
你有没有QQ给我留个号码
谢谢