vfp 动态列求和
有一临时表,数值型字段否固定, 有20至30列左右.请问怎样动态对数值型字段自动求和(纵向列求和)并以表格显示出来?
[解决办法]
请参考:http://topic.csdn.net/u/20100908/10/23f39be4-6d94-4f39-bb44-02a685e66235.html
[解决办法]
VFP6.0代码
MYFORM = NEWOBJECT("C_FORM")
MYFORM.SHOW
READ EVENTS
RETURN
DEFINE CLASS C_FORM AS FORM
AUTOCENTER=.T.
CAPTION="表单求和示例"
ADD OBJECT GRID1 AS GRID WITH TOP=((THISFORM.HEIGHT-THISFORM.GRID1.HEIGHT)/2)+20,LEFT=(THISFORM.WIDTH-THISFORM.GRID1.WIDTH)/2
ADD OBJECT OPTIONGROUP1 AS OPTIONGROUP WITH TOP=6,LEFT=THISFORM.GRID1.LEFT,HEIGHT=28,BUTTONCOUNT=2,AUTOSIZE=.T.
PROCEDURE OPTIONGROUP1.INIT
WITH THIS
.VALUE=0
.OPTION2.TOP=.OPTION1.TOP
.OPTION2.LEFT=.OPTION1.WIDTH+45
FOR I=1 TO THIS.BUTTONCOUNT
.BUTTONS(I).AUTOSIZE=.T.
.BUTTONS(I).CAPTION=IIF(MOD(I,2)=1,"统计","退出")
ENDFOR
ENDWITH
ENDPROC
PROCEDURE OPTIONGROUP1.CLICK
IF MYFORM.OPTIONGROUP1.BUTTONS(MYFORM.OPTIONGROUP1.VALUE).CAPTION=="统计"
*----方法一,数值型字段A1....An
GO BOTTOM
IF ALLTRIM(内容)!="合计" &&判断是否进行过"合计"
SUM TO ARRAY HJ &&字段数<255
INSERT INTO TABNAME (内容)VALUES ("合计")
FOR I=2 TO FCOUNT()
REPLACE (FIELD(I)) WITH HJ(I-1)
ENDFOR
ENDIF
*----方法二(SELECT SUM(字段名1),SUM(字段名5),SUM(字段名10),......FROM TABNAME INTO CURSOR LSB)
*----数值型字段不是相连的,可以自己研究。
MYFORM.GRID1.REFRESH
ELSE
MYFORM.RELEASE
ENDIF
ENDPROC
PROCEDURE GRID1.INIT
CREATE CURSOR TABNAME (内容 C(4),A1 N(4),A2 N(4),A3 N(4),A4 N(4)) &&字段数<255
INSERT INTO TABNAME VALUES ("A",15,16,17,18)
INSERT INTO TABNAME VALUES ("B",20,21,22,23)
GO TOP
WITH THIS
.FONTSIZE=10
.HEADERHEIGHT=28
.RECORDSOURCETYPE = 1
.RECORDSOURCE = "TABNAME"
.ROWHEIGHT=24
FOR I = 1 TO .COLUMNCOUNT
WITH .COLUMNS(I).HEADER1
.ALIGNMENT=2
ENDWITH
ENDFOR
ENDWITH
ENDPROC
PROCEDURE DESTROY
CLEAR EVENTS
ENDPROC
ENDDEFINE