Data retrieved using the data window in PowerBuilder is often unordered. Although you can set the SELECT statement to implement the sorting function, the data window cannot be dynamically adjusted once it is generated. I have summarized three methods for implementing dynamic sorting in the generated data window, And I will introduce them to you.
I. Preparations
Design the example window shown in 1. To better compare three different methods, the data in the dw-1 comes from two tables student and class. The student table contains four fields: Sid (student ID), sname (name), saddr (address), and CID (class number). The class table contains two fields: Cid (class number) and cname (class name ).
Figure 1
Source code of the two or three methods
The code for the "execute" button in the three methods is:
Method 1: Use setsqlselect ()
String LS-oldsql, LS-newsql, LS-order LS-Column Ls-oldsql = dw-1.getsqlselect () Choose case ddlb-1.text Case "student ID" ls-column = "Sid ″ Case "name" ls-column = "sname ″ Case "Address" ls-column = "saddr ″ Case "shift number" ls-column = "class. CID ″ Case "Class Name" ls-column = "cname ″ End choose If rb-1.checked then ls-order = "ASC ″ Else LS-order = "DESC ″ End if Ls-newsql = LS-oldsql + "order by" + & Ls-column + "" + LS-order If dw-1.setsqlselect (LS-newsql) =-1 then MessageBox ("warning", "data setting failed", stopsign !) Dw-1.settransobject (sqlca) Dw-1.reset () Dw-1.retrieve () Dw-1.setsqlselect (LS-oldsql) End if |
Method 2: Describe () and modify ()
String LS-mod, LS-order, LS-old, LS-Column Ls-old = dw-1.describe ('datawindow. Table. select ′) Dw-1.settransobject (sqlca) Choose case ddlb-1.text Case "student ID" ls-column = "Sid ″ Case "name" ls-column = "sname ″ Case "Address" ls-column = "saddr ″ Case "shift number" ls-column = "class. CID ″ Case "Class Name" ls-column = "cname ″ End choose If rb-1.checked then ls-order = "ASC ″ Else LS-order = "DESC ″ End if Ls-mod = "datawindow. Table. Select ='' "+ LS-old + & 'Order by "'+ LS-column +'" '+ LS-order + ″′″ Dw-1.modify (LS-mod) Dw-1.retrieve () Dw-1.modify ("datawindow. Table. Select = & ''' + LS-old + ″′″) |
Method 3: Use setsort () and sort ()
String LS-sort, LS-order, LS-Column Choose case ddlb-1.text Case "student ID" ls-column = "#1 ″ Case "name" ls-column = "#2 ″ Case "Address" ls-column = "#3 ″ Case "shift number" ls-column = "#4 ″ Case "Class Name" ls-column = "#5 ″ End choose If rb-1.checked then ls-order = "″ Else LS-order = "D ″ End if Ls-Sort = LS-column + ''+ LS-order Dw-1.setsort (LS-sort) Dw-1.sort () |
3. Comparison of the three methods
1. The first and second methods require that the data window be disordered during production, and the third method does not.
2. for the same column names from different forms (such as student. CID, class. CID) using the second method for sorting is troublesome, because you must pay special attention to the use of quotation marks when using the modify () function. However, the second method is faster than the first method.
3. The third method is most convenient to use. You can reference either a column name or a column number (for example, #4 indicates the fourth column) to specify a sequence.