想法:DataSource1组件、一个DBGrid1、ADOQuery。从Access数据库查询到数据后,经过统计计算得到一个数组,
怎样将这个数组的值添加到DBGrid中?刚学数据库,一头雾水,请指点
我的出错代码是:
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.active:=false;
ADOQuery1.SQL.Add(strSQL);
ADOQuery1.Open; Setlength(X,ADOQuery1.RecordCount);
Index := 0; //初始化下标
ADOQuery1.First;
While Not ADOQuery1.Eof Do //数据集循环
begin
X[Index] := ADOQuery1.FieldByName('销售额).asString;
Inc(Index);
ADOQuery1.Next;
end; iy1:=strtoint(copy(date1,1,4));
iy2:=strtoint(copy(date2,1,4));
for i:=iy1 to iy2 do
begin
for j:=1 to 12 do
begin
num := DaysInAMonth(i,j); //年月的天数
XX:=0; IDAY:=0;
Index := 0;
While IDAY <= num Do
begin
IDAY := IDAY + 1;
XX := XX + strtoint(X[Index]);
Inc(Index);
end;
Y[j] := XX/num;
end;
with DBGrid1.DataSource.DataSet do
begin
Append;
FieldByName('年份').Value := i;
FieldByName(' 1月').Value := Y[1];
FieldByName(' 2月').Value := Y[2];
FieldByName(' 3月').Value := Y[3];
FieldByName(' 4月').Value := Y[4];
FieldByName(' 5月').Value := Y[5];
FieldByName(' 6月').Value := Y[6];
FieldByName(' 7月').Value := Y[7];
FieldByName(' 8月').Value := Y[8];
FieldByName(' 9月').Value := Y[9];
FieldByName('10月').Value := Y[10];
FieldByName('11月').Value := Y[11];
FieldByName('12月').Value := Y[12];
Post;
end;
怎样将这个数组的值添加到DBGrid中?刚学数据库,一头雾水,请指点
我的出错代码是:
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.active:=false;
ADOQuery1.SQL.Add(strSQL);
ADOQuery1.Open; Setlength(X,ADOQuery1.RecordCount);
Index := 0; //初始化下标
ADOQuery1.First;
While Not ADOQuery1.Eof Do //数据集循环
begin
X[Index] := ADOQuery1.FieldByName('销售额).asString;
Inc(Index);
ADOQuery1.Next;
end; iy1:=strtoint(copy(date1,1,4));
iy2:=strtoint(copy(date2,1,4));
for i:=iy1 to iy2 do
begin
for j:=1 to 12 do
begin
num := DaysInAMonth(i,j); //年月的天数
XX:=0; IDAY:=0;
Index := 0;
While IDAY <= num Do
begin
IDAY := IDAY + 1;
XX := XX + strtoint(X[Index]);
Inc(Index);
end;
Y[j] := XX/num;
end;
with DBGrid1.DataSource.DataSet do
begin
Append;
FieldByName('年份').Value := i;
FieldByName(' 1月').Value := Y[1];
FieldByName(' 2月').Value := Y[2];
FieldByName(' 3月').Value := Y[3];
FieldByName(' 4月').Value := Y[4];
FieldByName(' 5月').Value := Y[5];
FieldByName(' 6月').Value := Y[6];
FieldByName(' 7月').Value := Y[7];
FieldByName(' 8月').Value := Y[8];
FieldByName(' 9月').Value := Y[9];
FieldByName('10月').Value := Y[10];
FieldByName('11月').Value := Y[11];
FieldByName('12月').Value := Y[12];
Post;
end;
想求每月总和可以用sum(销售额)...... group by 月份这样的代码实现,具体可以查sql语句的使用方法
在dbgrid中增加空白列也是可以的,不过利弊还是你自己体验一下吧
http://topic.csdn.net/t/20020519/00/733991.html