我用dbgrideh控件填充数据库no1-no30字段的数据后,要计算出这些字段所有不等于空的行数,并把计算结果加入对应列的页脚,代码如下:但得不到我要的结果,请大家帮忙看一下代码有什么问题?怎样修改才能达到我要的结果。
var
i,j:integer;
begin
ADODataSetFenbu.DisableControls;
for j := 1 to 30 do
begin
i := 0;
ADODataSetFenbu.first;
while not ADODataSetFenbu.eof do
begin
if ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString <> '' then
i := i + 1;
ADODataSetFenbu.next;
end;
DBGridEhFenbu.columns[j].footers[0].valuetype := fvtStatictext;
DBGridEhFenbu.columns[j].footers[0].value := inttostr(i);
end;
ADODataSetFenbu.EnableControls;
end;
var
i,j:integer;
begin
ADODataSetFenbu.DisableControls;
for j := 1 to 30 do
begin
i := 0;
ADODataSetFenbu.first;
while not ADODataSetFenbu.eof do
begin
if ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString <> '' then
i := i + 1;
ADODataSetFenbu.next;
end;
DBGridEhFenbu.columns[j].footers[0].valuetype := fvtStatictext;
DBGridEhFenbu.columns[j].footers[0].value := inttostr(i);
end;
ADODataSetFenbu.EnableControls;
end;
2.ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString 建议加上trim(),防止空格
1.你要计算的是行数,外层的循环用while not ADODataSetFenbu.eof do,然后逐个计算字段
2.ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString 加上trim(),防止空格
可以直接用SQL查詢得到,要排除空格就加上ltrim()select count(1) cnt1 from t where len(isnull(No1,''))<>0
或
select sum(case when len(isnull(No1,''))=0 then 0 else 1 end) as cnt1,
sum(case when len(isnull(No2,''))=0 then 0 else 1 end) as cnt2,
...
from t
ADODataSetFenbu.append;
中间代码这段怎么写?
ADODataSetFenbu.post;
var
i,j:integer;
begin
ADODataSetFenbu.DisableControls;
for j := 1 to 30 do
begin
i := 0;
ADODataSetFenbu.first;
while not ADODataSetFenbu.eof do
begin
if ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString <> '' then
i := i + 1;
ADODataSetFenbu.next;
end;
//DBGridEhFenbu.columns[j].footers[0].valuetype := fvtStatictext;
//DBGridEhFenbu.columns[j].footers[0].value := inttostr(i);
ADODataSetFenbu.append;
ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString := inttostr(i);
ADODataSetFenbu.post;
end;
ADODataSetFenbu.EnableControls;
end;
所得到的各列行数不是显示在新增的这一行,而是每列行数依次向右下递推了一个单元格,怎么回事啊?