动态创建数据窗口的两种方法

    科技2025-08-08  13

    创建数据有多种方法,比如用SELECT语句创建,这里最简单,还有用自定义字创建数据窗口,这个比较复杂,

    下面就这两种方法介绍一下:

    1、用SELECT创建数据窗口

         string ls_sql = 'select * from mara'      string error_syntaxfromSQL, error_create

         string new_syntax

         new_syntax = SQLCA.SyntaxFromSQL(ls_sql, 'Style(Type=grid)', error_syntaxfromSQL)         IF Len(error_syntaxfromSQL) > 0 THEN          // Display errors          messagebox('创建失败[1]!!!','错误创建语句:'+error_syntaxfromSQL)          return     ELSE          // Generate new DataWindow          dw_1.Create(new_syntax, error_create)          IF Len(error_create) > 0 THEN              messagebox('创建失败[2]!!!','创建数据窗口出错:'+ error_create)              return          END IF

        END IF

        string ls_request, ls_report     ls_report = dw_1.Describe("Datawindow.Objects") +"~t"     long Ll_Pos,ll_start     string ls_objstr,ls_obj     string ls_colname[]     long ll_count     Ll_Pos =  Pos(Ls_ObjStr, "~t", Ll_Start)

     

        //设置数据窗口更新字段     long Ll_Pos,ll_start =1     string ls_objstr,ls_obj     string ls_colname[]     long ll_count     Ls_ObjStr = DW_1.Describe("Datawindow.Objects") + "~t"     Ll_Pos =  Pos(Ls_ObjStr, "~t", Ll_Start)

        Do While Ll_Pos > 0          Ls_Obj = Mid(Ls_ObjStr, Ll_Start, Ll_Pos - Ll_Start)          If (DW_1.Describe(Ls_Obj + ".type") = 'column' ) And (DW_1.Describe(Ls_Obj + ".band") = 'detail') And &              (DW_1.Describe(Ls_Obj + ".visible") = "1" ) Then             ll_count ++             ls_colname[ll_count] = Trim(Ls_Obj)          End if          Ll_Start = Ll_Pos + 1          Ll_Pos =  Pos(Ls_ObjStr, "~t", Ll_Start)     loop

        for ll_count =1 to upperbound(ls_colname)          DW_1.MODIFY('update=yes updatewhereclause=yes key=yes name='+ls_colname[ll_count]+' dbname="'+ls_colname[ll_count]+'"')     next

        DW_1.MODIFY('updatewhere=1 updatekeyinplace=no')

        DW_1.SetTransObject(SQLCA)

    2、用自定义字段创建数据窗口

         //定义全局变量

         string is_colname[] = {'col1','col2','col3'}                           //所有字段名      string is_headtxt[] = {'参数1','参数2','参数3'}                     //所有字段对应的头文本      string is_colsize[] = {'char(20)','number','decimal(0)'}      //所有字段对应的类型      string is_font_face = '幼圆'                                                //显示的字体      boolean ib_edit = TRUE                                                     //列是否可编辑

         //实现代码

         string ls_sql,ls_error,ls_tempsize      long   ll_i      long   ll_x,ll_width,ll_tabseq

         //说明版本及数据窗口的属性      ls_sql = 'release 9;~r~n'+&                 +'datawindow ( units=0 timer_interval=0 color=1073741824 processing=1 print.printername="" '+&                 +'print.documentname=""  print.orientation=0 print.margin.left=110 print.margin.right=110 '+&                 +'print.margin.top=96 print.margin.bottom=96 print.paper.size=0 print.paper.source=0 '+&                 +'print.canusedefaultprinter=yes grid.lines=0 selected.mouse=yes)'

         ls_sql = ls_sql + + '~r~n' + &                 +'header(height=92 color="536870912" )'+&                 +'summary(height=0 color="536870912" )'+&                 +'footer(height=0 color="536870912" )'+&                 +'detail(height=100 color="536870912" )'

         //声明数据窗口的所有字段      ls_sql = ls_sql + '~r~ntable('      for ll_i = 1 to upperbound(is_colname) step 1           ls_sql = ls_sql +'column=(type='+string(is_colsize[ll_i])+' updatewhereclause=yes name='+is_colname[ll_i]+' dbname="'+is_colname[ll_i]+'"  )~r~n'      next      ls_sql = ls_sql + ')~r~n'

         //设置所有列的抬头      ll_width = 0      ll_X     = 9      for ll_i = 1 to upperbound(is_colname) step 1           ll_x  = ll_x + 9 +ll_width               if lower(left(is_colsize[ll_i],4)) = 'char' then              ls_tempsize = mid(is_colsize[ll_i],pos(is_colsize[ll_i],'(')+1)              ll_width = long(mid(ls_tempsize,1,pos(ls_tempsize,')')-1))          else              ll_width = 10          end if          ll_width = ll_width * 32          ls_sql = ls_sql + &                     +'text(name='+is_colname[ll_i]+'_t visible="1" band=header font.charset="0" '+&                     +'font.face="'+is_font_face+'" font.family="2" font.height="-12" font.pitch="2" '+&                     +'font.weight="400" background.mode="1" background.color="536870912" color="33554432" '+&                     +'alignment="2" border="0" x="'+string(ll_x)+'" y="8" height="76"'+&                     +'width="'+string(ll_width)+'" text="'+is_headtxt[ll_i]+'" )~r~n'

         next      ls_sql = ls_sql +'~r~n'

         //设置所有列的属性      ll_width = 0      ll_x     = 9      for ll_i = 1 to upperbound(is_colname) step 1           ll_x  = ll_x + 9 +ll_width           if lower(left(is_colsize[ll_i],4)) = 'char' then              ls_tempsize = mid(is_colsize[ll_i],pos(is_colsize[ll_i],'(')+1)              ll_width = long(mid(ls_tempsize,1,pos(ls_tempsize,')')-1))          else              ll_width = 10          end if          ll_width = ll_width * 32             if ib_edit then  //设置可编辑             ll_tabseq = ll_i * 10         end if            ls_sql = ls_sql + &                    +'column(name='+is_colname[ll_i]+' visible="1" band=detail id='+string(ll_i)+&                    +' x="'+string( ll_x)+'" '+&                      +'y="8" height="88"'+&                    +'width="'+string(ll_width)+'" color="33554432" border="0" alignment="0" format="[general]" '+&                    +'edit.focusrectangle=no edit.autohscroll=yes edit.autoselect=yes edit.autovscroll=no edit.case=any edit.codetable=no '+&                    +'edit.displayonly=no edit.hscrollbar=no edit.imemode=0 edit.limit=0 edit.password=no edit.vscrollbar=no edit.validatecode=no '+&                    +'edit.nilisnull=no edit.required=no criteria.required=no criteria.override_edit=no crosstab.repeat=no background.mode="1" '+&                    +'background.color="536870912" font.charset="0" font.face="'+is_font_face+'" font.family="2" font.height="-12" font.pitch="2" '+&                    +'font.weight="400" tabsequence='+string(ll_tabseq)+' )~r~n'     next     ls_sql = ls_sql +'~r~n'

        ls_sql = ls_sql +&                +'htmltable(border="1" )~r~n'+&                +'htmlgen(clientComputedFields="1" clientEvents="1" clientFormatting="0" clientScriptable="0" '+&                +'clientValidation="1" generateJavaScript="1" netscapeLayers="0" netscapelayers="0" encodeSelfLinkArgs="1" )~r~n'+&                +'export.xml(headGroups="1" includewhitespace="0" metadatatype=0 savemetadata=0 )~r~n'+&                +'import.xml()~r~n'+&                +'export.pdf(method=0 distill.customPostScript="0" xslfop.print="0" )~r~n'

        dw_1.Create(ls_sql,ls_error)    //创建数据窗口     if ls_error <> '' and not isnull(ls_error) then        messagebox('error',ls_error,stopsign!)       end if

        messagebox('','Success') 

    Processed: 0.010, SQL: 8