如题
先简介一下我的程序,先从excel导入一批数据,然后对每一行数据进行分别对比,若该行中某个数据大于该行的平均数,则背景标记为淡绿色,字体为红色。
我曾用过ondrawcell来做,却达不到我的目的
请各位想想办法,解决一下!!
先简介一下我的程序,先从excel导入一批数据,然后对每一行数据进行分别对比,若该行中某个数据大于该行的平均数,则背景标记为淡绿色,字体为红色。
我曾用过ondrawcell来做,却达不到我的目的
请各位想想办法,解决一下!!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
i,j: integer;
begin
Self.StringGrid1.RowCount := 11;
Self.StringGrid1.ColCount := 11;
for i:=0 to 10 do
for j:=0 to 10 do
StringGrid1.Cells[i,j] := inttostr(i*j);
end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
V: string;
begin
V := StringGrid1.Cells[ACol, ARow];
if strtoint(StringGrid1.Cells[ACol, ARow])>20 then
StringGrid1.Canvas.Brush.Color := clRed
else
StringGrid1.Canvas.Brush.Color := clWhite; StringGrid1.Canvas.FillRect(Rect); DrawText( StringGrid1.Canvas.Handle,
PChar(V),
Length(V)+1,
Rect,
0);
end;end.
//---------------
维护一个二维数组(或其它结构/如链表), 保存click标记的单元格坐标.
只是ondrawcell根据一个bool标志决定是否变色
click点击则改变这个bool标志
要你那样也行,在导入的同时进行计算,然后再设标志状态 不难实现
标准的stringgrid,只能在ondrawcell时画颜色,是否每次都需要计算一次才设颜色?好像会效率略低,不过应该也不要紧,因为只计算同一行的20列