假设有七个字段,则首先设置Colcount属性为7。
然后分为如下几步:
1、加标题,控制列距离:
procedure init_grid(sgrid:Tstringgrid);
begin
with sgrid do
begin
ColWidths[0] := 50;
ColWidths[1] := 350;
ColWidths[2] := 70;
ColWidths[3] := 30;
ColWidths[4] := 115;
ColWidths[5] := 50;
ColWidths[6] := 50;
Cells[0,0] := '序号';
Cells[1,0] := '标题';
Cells[2,0] := '来自';
Cells[3,0] := '分数';
Cells[4,0] := '最后回复时间';
Cells[5,0] := '回复次数';
Cells[6,0] := '状态';
end;
end;
2、清空数据:
procedure Clear_grid(sgrid:Tstringgrid);
begin
with sgrid do
begin
RowCount := 2;
FixedRows := 1;
Cells[0,1] := '';
Cells[1,1] := '';
Cells[2,1] := '';
Cells[3,1] := '';
Cells[4,1] := '';
Cells[5,1] := '';
Cells[6,1] := '';
end;
sgrid.TopRow := 1;
init_grid(sgrid);
sgrid.update;
end;
3、填充数据:
procedure Fill_grid(query1:Tquery;sgrid:Tstringgrid);
var LastRow,i1:integer;
begin
if not query1.active then abort;
query1.first;
i1:=0;
while not query1.eof do
begin
inc(i1);
Cells[0,i1] := DSLocal.Fields[0].value;
Cells[1,i1] := DSLocal.Fields[1].value;
Cells[2,i1] := DSLocal.Fields[2].value;
Cells[3,i1] := DSLocal.Fields[3].value;
Cells[4,i1] := DSLocal.Fields[4].value;
Cells[5,i1] := DSLocal.Fields[5].value;
Cells[6,i1] := DSLocal.Fields[6].value;
query1.next;
end;
sgrid.update;
end;