我用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; 

解决方案 »

  1.   

    1.你要计算的是行数,那么外面的循环应该是while not ADODataSetFenbu.eof do,然后每个字段检查
    2.ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString 建议加上trim(),防止空格
      

  2.   

    好吧 再回一次
    1.你要计算的是行数,外层的循环用while not ADODataSetFenbu.eof do,然后逐个计算字段
    2.ADODataSetFenbu.FieldByName('No'+inttostr(j)).AsString  加上trim(),防止空格
      

  3.   

    应该没问题,可能是字段有空格的情况,没排除到
    可以直接用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
      

  4.   

    不会是空格问题吧,我所有填充字段的的行值都是同一个字符串 T1,两外我还有一行页脚是显示其他统计值的,不会是dngtideh不支持两行页脚吧
      

  5.   

    如果我不是把计算的每列行数值用页脚显示,而换成在表格最下方新增一行,该行用于代替页脚显示各列不为空的行数值,怎么做?
    ADODataSetFenbu.append;
    中间代码这段怎么写?
    ADODataSetFenbu.post;
      

  6.   

    按照楼上的思路,加上如下代码
    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; 
    所得到的各列行数不是显示在新增的这一行,而是每列行数依次向右下递推了一个单元格,怎么回事啊?