Populate dropdownlistbox items from SQL table in P

2019-09-08 18:51发布

问题:

I have been working on Vb.net but i switched to PowerBuilder 12.5 Classic, I am finding my way around. I need to know the equivalent PowerBuilder script;

  1. to clear a control "textbox1.clear()"
  2. to focus "textbox1.focus()"
  3. to insert SQL database items to drop-down listbox
    While dr.Read
       ComboBox1.Items.Add(dr("itemname"))
    end while

回答1:

  1. textbox1.Reset()
  2. textbox1.SetFocus()
  3. example (untested, but it shows the idea):
    string ls_sql, ls_syntax, ls_errors

    ls_sql = "select name from users"
    ls_syntax = sqlca.SyntaxFromSql(ls_sql, "", ls_errors)
    if len(ls_errors) > 0 then return

    datastore ds
    ds = create datastore
    ds.create(ls_syntax, ls_errors)
    if len(ls_errors) = 0 then
        ds.SetTransObject(sqlca)
        ds.Retrieve()

        long ll_row,ll_rows
        string ls_val
        ll_rows = ds.RowCount()
        for ll_row = 1 to ll_rows
            ls_val = ds.GetItemString(ll_row, "name")
            Combobox1.AddItem(ls_val)
        next

    end if

    destroy ds

Edit some comments: as Terry says in its answer, the Datawindow and the DataStore are the key controls of Powerbuilder. Consider the DataStore as a VB recordset and the DW is a kind of visual recordset (that can show the data in the way of a form, a grid, ...).

I answered you question by using a DS to retrieve the data and to easily iterate on it (it is easier to manipulate than a cursor) and I translated the filling of the combobox. But as Terry said, you should study how to use a DropDownDataWindow that is way more powerful and evolutive.



回答2:

To answer while completely ignoring your questions...

Keep in mind that the power of PowerBuilder is in the DataWindow. Loading a dropdown from the database is as simple as setting some attributes on a DataWindow column by making the column a DropDownDataWindow edit style. Depending on what you need, after the DDDW attributes are set, the DDDW values will be loaded when the primary DataWindow is retrieved, no code required (beyond that to set up the database connection with the primary DataWindow and to retrieve the primary DataWindow). In fact, the DDDW gives additional power over a combo box, as its dropdown is another DataWindow, so it can have multiple columns, headers, graphics, row-based expressions, conditional colouring, item "rows" that are multiple text lines tall, etc.... You leave yourself open to more opportunities by going with the DataWindow. (I know. I walked out of my Intro to PB course, thought DWs were overblown, and tried to program without them. Learned an appreciation for all they do very quickly. Gave up my non-DW approach on the next project.)

Good luck,

Terry