1
votes

How to create dynamic DataWindow in PowerBuilder?

I have procedure in database which return me a cursor. How with this cursor I can create DataWindow with columns?

Example1: Cursor:

SELECT DATA, ID FROM TABLE1
I want DW with columns:
DATA, ID

Example2: Cursor:

SELECT DATA, ID, COUNTRY FROM TABLE1
I want DW with columns:
DATA, ID, COUNTRY

How to create that?

3

3 Answers

2
votes

what you need is the Syntaxfromsql function. You can read it more here:

http://infocenter.sybase.com/archive/index.jsp?topic=/com.sybase.help.pb_10.5.psref/html/psref/CCJCHAJI.htm

for your example:

string  ls_sql_syntax, ls_dw_source, ERRORS

ls_sql_syntax   = "SELECT DATA, ID, COUNTRY FROM TABLE1"
ls_dw_source    = SQLCA.SyntaxFromSQL(ls_sql_syntax, "Grid", ERRORS)
IF Len(ERRORS) > 0 THEN
    MessageBox("Caution", "SyntaxFromSQL caused these errors: " + ERRORS)
    RETURN 0
END IF
dw_work.Create( ls_dw_source, ERRORS)
IF Len(ERRORS) > 0 THEN
    MessageBox("Caution", "Create cause these errors: " + ERRORS)
    RETURN 0
END IF  

Where we suppose that dw_work is the datawindow control's name. After creating this "basic" datawindow you can customize it at runtime with the Modify methods. You can set nearly everything (width, height, font size, color etc.)

More info here:

http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc00044_0250/html/dwprgnet/X_ref299343589.htm

I hope that helps. Please feel free to ask more. We are using this dynamic datawindow concept in our project heavily.

Br. Gábor

2
votes

You can easily dynamically a dataobject with the DataWindow's or DataStore's Create() function, passing it a syntactically correct DataWindow source (like what you see when you Edit Source on a DataWindow in the IDE). There's even a function, the Transaction object's SyntaxFromSQL(), that will help you build a syntax from a SELECT statement, although you can build this syntax string however you'd like.

The problem I see in your question is that you're not starting from a SELECT statement, but from a cursor object passed back from the database. I don't believe there is any support anywhere in PB for such a cursor. (You can create a cursor within your PowerScript, but as I understand what you're saying, that is different.) So, since there is no DataWindow syntax that is compatible with a DBMS-generated cursor, I believe the answer to your question as it stands is that it can't be done.

One alternative, if you have control over the stored procedure, is to change it to pass back the string of the SELECT statement. This way, not only can you dynamically create the DataWindow syntax (SyntaxFromSQL()) and generate the DataWindow (Create()), but you'll also get better performance, since the DataWindow in most cases (e.g. not when a RetrieveRow event is coded) will retrieve the entire data set in as few network blocks as possible with "one" network transaction, as opposed to the cursor which will only retrieve one row at a time.

Good luck,

Terry.

0
votes

You can add to your framework a function which returns a DataStore created dynamically by the supplied SQL SELECT. Its code is here.