PowerBuilder Authors: Chris Pollach, Yeshim Deniz, Jayaram Krishnaswamy, Kevin Benedict, Avi Rosenthal

Related Topics: PowerBuilder

PowerBuilder: Article

DataWindow.NET How To: Data Entry Form

Simplifying database application development

Similar to the methods used to populate the dropdownlistboxes, the VB.NET PopulateProductList method has approximately 50 lines of code used to populate the product listbox. As shown below, the DW.NET version has 18 lines of code in which we establish an ADO.NET connection, associate it with a Sybase.DataWindow.AdoTransaction object, assign that to the DataWindowControl, and then retrieve the DataWindow control. We don't have to worry about copying the data from an SQLReader to our display control as the VB.NET version does. That's all handled in the DataWindow Object.

DW.NET PopulateProductList
Dim conn As New System.Data.OleDb.OleDbConnection
conn.ConnectionString() =
Dim SQLCA As New Sybase.
SQLCA.Connection = conn
IsRetrieving = True
IsRetrieving = False
If dw_productlist.RowCount() > 0
dw_productlist.SelectRow(1, True)
End If
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.
Critical, "General Error")
End Try

The one unusual thing here is the IsRetrieving variable we set before and after retrieving the DataWindowControl. To understand why that's there, we have to look at how both forms select a product in the product list (including the initial selection of the first item by the PopuplateProductList method).

In the VB.NET form, the lstProducts_SelectedIndexChanged method is used to respond to the selection of a product in the product list. Because the other controls on the form are separate controls, the VB.NET method calls a PopulateForm method - another 65 lines of code that are needed to retrieve the additional product data from the database and populate those controls with it. The VB.NET form then sets the Mode variable (remember that it's manually tracking whether the user is adding a new record or editing an existing one) and enables the Add and Delete command buttons.

In the DW.NET form, two methods are used to respond to the selection of a product in the product list. The dw_productlist_RowFocusChanging method (as shown below) is called before the new product is made the current product in the product list. We're going to attempt to save any changes the user made to the original record before letting him to move to another item. So we do an AcceptText on the DataWindowControl and then, if the ModifiedCount is greater than 0, call the UpdateProduct method of the form to save the data. The AcceptText method ensures that the last data entry made by the user has been accepted into the control before attempting the update. If the update succeeds, the two calls to the SelectRow method of the DataWindowControl set the selection indicator to the newly selected product. The first call to the function de-selects that indicator for the entire control (all rows); otherwise the row selection indicator would appear on several rows.

If Not dw_product.AcceptText() Then
e.Cancel = True
Exit Sub
End If
If (dw_product.ModifiedCount() > 0) Then
End If
dw_productlist.SelectRow(0, False)
dw_productlist.SelectRow(e.NewRowNumber, True)

The dw_productlist_RowFocusChanged method (as shown below) is called after the newly selected product becomes the current product. Here we simply scroll the active row in the free form DataWindowControl to make that product visible (the free form DataWindowControl only shows one row at a time). Remember that the dw_productlist DataWindowControl has all of the rows of product data in its memory and the dw_product DataWindowControl is sharing that same data (pointed to that same area of memory). The DataWindowControls not only display the data differently; they can also display different rows of data; so the purpose of the dw_productlist_RowFocusChanged method is to keep them in synch (showing the same product).

If e.RowNumber > 0 And Not
IsRetrieving Then
End If

Here's where that IsRetrieving variable comes in. The dw_productlist_ RowFocusChanging method isn't just called when the user selects a new product or the PopulateProductList selects the first item as the default. It's also called when the DataWindowControl is first retrieved. That's too soon for us though, and actually causes some problems. So we set the IsRetrieving variable to indicate when a retrieve is happening, and then tell the dw_productlist_RowFocusChanged method to ignore the request if that's the reason for the call.

More Stories By Bruce Armstrong

Bruce Armstrong is a development lead with Integrated Data Services (www.get-integrated.com). A charter member of TeamSybase, he has been using PowerBuilder since version 1.0.B. He was a contributing author to SYS-CON's PowerBuilder 4.0 Secrets of the Masters and the editor of SAMs' PowerBuilder 9: Advanced Client/Server Development.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
alvin 07/23/08 08:36:24 PM EDT

how can i change the highlight color of selected row? the default is dark blue which is too heavy. thanks

Allan A. Cartagenas 08/18/05 04:38:53 AM EDT

Where can I download the file frmDWMain.vb? The link to download the file is not included on the article.