PB 动态外部创建DataWindow数据窗口的方法
首先制作一个自己想动态得到的数据窗口,然后将该数据窗口导出,看看语法,这样你就了解了这种数据窗口的生成语法了!
dw_1.create() 可以参考帮助 以及 srd文件(数据窗口导出文件)
一、动态数据窗创建原理及实现
在实际应用中,经常需要根据用户需求来动态创建数据窗,一般方法是这样的。
在一个window中加入一个数据窗控件,如dw_new,但是该数据窗没有dataobject,就可以用以下语法来创建:
dw_new.create(ls_syntax,ls_error) // 创建语法,错误信息
二、ls_syntax可以用以下三种方法来实现
2.1 由sql语法创建:
// 连接到pb的example数据库
string ls_sql,ls_syntax,ls_error ls_syntax = 'select * from department' ls_syntax = sqlca.SyntaxFromSQL(ls_sql,'style(type=grid)',ls_error) if len(ls_error) >0 then messagebox('Error','SyntaxFromSQL Error:~r'+ls_error) else dw_new.create(ls_syntax,ls_error) if len(ls_error) >0 then MessageBox("Error", "Create have these errors: ~r" + ls_error) else dw_new.settrans O B J E C T(sqlca) dw_new.retrieve() end if end if
2.2由另一个数据窗的syntax来创建
string ls_syntax,ls_error ls_syntax = dw_test.describe('datawindow.syntax') dw_new.create(ls_syntax,ls_error) if ls_error <> '' then messagebox('Create Error',ls_error) else dw_new.settrans O B J E C T(sqlca) dw_new.retrieve() end if
2.3读取psr文件来创建
该方法可以实现后期展示数据的动态改变
string ls_syntax,ls_error,ls_ret ls_ret = char(13)+char(10) //回车键 int li_fileNum long li_length li_FileNum = FileOpen("efef.psr",Streammode!, read!, shared!, Replace!) // 以下是pb5的代码 if li_filenum >0 then FileSeek(li_FileNum, 158, FromBeginning!) li_length = fileRead(li_filenum,ls_syntax) end if fileclose(li_filenum) if li_length = 0 then return ls_syntax = "release 5;"+ls_ret+ls_syntax //截掉ls_syntax中的数据部分,5.0以"sparse(names="dept_name?) "作为参考位置 //6.0以html(作为参考位置 long pos1,pos2 pos1 = pos(ls_syntax,'sparse(names="',1) pos2 = pos(ls_syntax,'"',pos1 +16) ls_syntax = left(ls_syntax,pos1) + mid(ls_syntax,pos1 +1,pos2 - pos1 +1) dw_New.create(ls_syntax,ls_error) if ls_error <> '' then messagebox('Create Error',ls_error) else dw_new.settrans O B J E C T(sqlca) dw_new.retrieve() end if global type f_createextenddw from function_ O B J E C T end type forward prototypes global function string f_createextenddw (ref datawindow dw, string cols[]) end prototypes global function string f_createextenddw (ref datawindow dw, string cols[]);string sql_dw_general, sql_dw_columns_type, sql_dw_headers_conf, sql_dw_columns_conf, ls_errors int i long ll_colcount string ls_coln ametype string ls_colname,ls_coltype long ll_pos ll_colcount = upperbound(cols) //generals sql_dw_general = "release 8;" & + "datawindow(units=0 timer_interval=0 color=16777215 processing=0 print.documentname=" + char(34) + "" + char(34) + " print.orientation = 0 print.margin.left = 110 print.margin.right = 110 print.margin.top = 96 print.margin.bottom = 96 print.paper.source = 0 print.paper.size = 0 print.prompt=no print.buttons=no print.preview.buttons=no )" & + "header(height=72 color=" + char(34) + "536870912" + char(34) + " )" & + "summary(height=0 color=" + char(34) + "536870912" + char(34) + " )" & + "footer(height=0 color=" + char(34) + "536870912" + char(34) + " )" & + "detail(height=84 color=" + char(34) + "536870912" + char(34) + " )" //列及类型 sql_dw_columns_type = "table(" FOR I=1 to ll_colcount ls_colnametype = trim(cols) ls_colname = right(ls_colnametype,len(ls_colnametype) - 1) ls_coltype = left(ls_colnametype,1) choose case upper(ls_coltype) case 'C' ls_coltype = 'char(100)' case 'D' ls_coltype = 'date' case 'N' ls_coltype = 'decimal(6)' case else ls_coltype = 'char(100)' end choose //ls_coltype = 'char(100)' //列及类型 sql_dw_columns_type= sql_dw_columns_type + "column=(type=" +ls_coltype+" updatewhereclause=no name=" + ls_colname + " dbname=" + char(34) + ls_colname + char(34) + " ) " //列标题 sql_dw_headers_conf = sql_dw_headers_conf + "text(band=header alignment=" + char(34) + "2" + char(34) + " text=" + char(34) + ls_colname + char(34) + " border=" + char(34) + "6" + char(34) + " color=" + char(34) + "16711680" + char(34) + " x=" + char(34) + string( ((i - 1) * 588) + 5) + char(34) + " y=" + char(34) + "4" + char(34) + " height=" + char(34) + "64" + char(34) + " width=" + char(34) + "574" + char(34) + " name=" + ls_colname + "_t font.face=" + char(34) + "Arial" + char(34) + " font.height=" + char(34) + "-10" + char(34) + " font.weight=" + char(34) + "400" + char(34) + " font.family=" + char(34) + "2" + char(34) + " font.pitch=" + char(34) + "2" + char(34) + " font.charset=" + char(34) + "0" + char(34) + " background.mode=" + char(34) + "2" + char(34) + " background.color=" + char(34) + "12632256" + char(34) + " )" //列属性 sql_dw_columns_conf = sql_dw_columns_conf + "column(band=detail id=" + string(i) + " alignment=" + char(34) + "1" + char(34) + " tabsequence=" + string(i) + "0 border=" + char(34) + "2" + char(34) + " color=" + char(34) + "0" + char(34) + " x=" + char(34) + string( ((i - 1) * 588) + 5) + char(34) + " y=" + char(34) + "4" + char(34) + " height=" + char(34) + "76" + char(34) + " width=" + char(34) + "579" + char(34) + " format=" + char(34) + "[general]" + char(34) + " name=" + ls_colname + " edit.limit=0 edit.case=any edit.autoselect=yes edit.autohscroll=yes font.face=" + char(34) + "Arial" + char(34) + " font.height=" + char(34) + "-10" + char(34) + " font.weight=" + char(34) + "400" + char(34) + " font.family=" + char(34) + "2" + char(34) + " font.pitch=" + char(34) + "2" + char(34) + " font.charset=" + char(34) + "0" + char(34) + " background.mode=" + char(34) + "1" + char(34) + " background.color=" + char(34) + "536870912" + char(34) + " )" NEXT sql_dw_columns_type= sql_dw_columns_type + " )" sql_dw_columns_conf = sql_dw_columns_conf + "htmltable(border=" + char(34) + "1" + char(34) + " cellpadding=" + char(34) + "0" + char(34) + " cellspacing=" + char(34) + "0" + char(34) + " generatecss=" + char(34) + "no" + char(34) + " nowrap=" + char(34) + "yes" + char(34) + ")" //MESSAGEBOX('',sql_dw_general + sql_dw_columns_type + sql_dw_headers_conf + sql_dw_columns_conf) dw.Create(sql_dw_general + sql_dw_columns_type + sql_dw_headers_conf + sql_dw_columns_conf , ls_errors) return(ls_errors) end function