HowToCreateAFormWithAGridAndPopulateItWithData

From Dabo Wiki
Jump to: navigation, search

First, create a directory to hold the files. I'm calling mine test.

Next, define the connection to the database by running the CxnEditor.py script in the Dabo IDE directory. I'm defining a connection to the public MySQL webtest database on the dabodev.com server. Once the connection is working (click the Test... button to check it!), click the Save button to save the connection information into your app directory. It creates a small XML file with a .cnxml extension that holds all the information Dabo needs to connect to that database. Make sure to give the connection a meaningful name, since that's what is used to load it later on; I called mine main.

OK, now we're ready to fire up the Class Designer. I'm going to add a grid to the form by right-clicking on the main surface, and selecting Data Controls/ Add Grid from the popup menu, and accepting the default of 3 columns for the grid. I then select the Property Sheet, which shows that dGrid is the current selection. I want to give the grid a RegID, so that I can more easily refer to it in code, so I enter gridZip as the value in the RegID property.

Next I want to customize the Columns. I can select each by clicking on them in the Designer surface, or by selecting them in the Object Tree, or by pressing Ctrl-PageUp/Down (Cmd-PageUp/Down on OSX) to change the selected object. For each column I want to set the Caption property to something meaningful, and set the DataField property to the name of the field in the zipcodes table that will be displayed in that column. So the columns are set so that their Captions are City, State and Zip, respectively, while the DataField properties are set to ccity, cstateprov and czip.

If desired, you can set the column widths by dragging the vertical separator lines in the Designer surface, or by manually setting the Width property in the prop sheet for each.

Now comes the fun part! Select the Code Editor window, and with the Object dropdown set to the form, select afterInitAll from the Method dropdown.

What we want to do is get the connection, create the bizobj from that connection, configure it to get the data we want, and then populate the grid with that data. Here's the afterInitAll() code that is needed to do just that; the comments explain what each bit of code does.

def afterInitAll(self):
    # The app may automatically load this connection definition,
    # but just in case it isn't in the path, we can load it manually. No
    # connection is made, so there is no harm in adding a connection
    # definition more than once.
    self.Application.addConnectFile("test.cnxml")

    # This call will actually create the connection if it hasn't already
    # been made. If it has, it returns the existing connection, so that 
    # multiple connections aren't used up.
    conn = self.Application.getConnectionByName("main")
    # Now create the bizobj, using the connection.
    biz = dabo.biz.dBizobj(conn)
    # These two properties are essential for every bizobj.
    biz.DataSource = "zipcodes"
    biz.KeyField = "iid"
    # Add it to the form's collection of bizobj references
    self.addBizobj(biz)

    # Now set up the fields 
    biz.addField("iid")
    biz.addField("ccity")
    biz.addField("cstateprov")
    biz.addField("czip")
    # Add a WHERE clause to limit the result set
    biz.addWhere("ccity = 'Springfield' ")
    # Run the query
    self.requery()

That's it! Save the class design into your app directory. You can test it directly from the Class Designer by selecting Run... from the File menu, or you can select Save Runnable App to create a .py file you can run directly. Either way, when you run the form, you'll have a fully-functional grid, populated with the data you specified.