Browse DevX
Sign up for e-mail newsletters from DevX


Sybase's DataWindow .NET: Way Beyond the .NET DataGrid : Page 6

Sybase's new DataWindow .NET lets you create powerful data-driven applications that provide advanced display and printing capabilities with less programming than ever. If you ever worked with PowerBuilder, you'll know why a .NET version of the DataWindow is such an important addition to your toolset. If not, you'll be surprised at the power of the DataWindow.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Saving Changed Data
You've now created a data entry form, but haven't provided the application with the ability to update the database. The DataWindow won't save changes until you add a little more code. The following try...catch block commits the data only if the user's changes are valid. Before saving, the code calls the AcceptText() method, which captures all pending changes if users have not moved the cursor off the last column they changed.

//get all changes pending in the buffer if the user //has not tabbed away from the last column changed dwc1.AcceptText(); try { //try to update dwc1.UpdateData(); //commit the changes transaction1.Commit(); } catch (Sybase.DataWindow.DbErrorException ex) { MessageBox.Show(ex.SqlErrorText+"\n"+ "Changes Not Saved", "Database Error Encountered"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Unexpected Error Encountered"); }

Adding New Rows
You add new rows to the DataWindow by calling the InsertRow() method. Supply 0 as the argument to add the row at the bottom, or supply a number less than the total number of rows to insert a blank row between existing rows. After inserting the new row, you will probably want to scroll to the row. You could highlight the row, but for now, just let the user click in the row and start entering data. Add the following code to the ButtonAdd_Click method.

//ButtonAdd_Click event: //add a row to end (0=add to end) dwc1.InsertRow(0); //get the count of rows and go //to the last row. dwc1.ScrollToRow(dwc1.RowCount);

Deleting Rows
Here's some quick code to delete a row when a user clicks in a row and then clicks the Delete button. This sample simply displays the deleted row, but ideally, you'd present a warning message with better information before issuing the DeleteRow() method. Remember, the DataWindow doesn't physically delete the row from the database unless the user clicks Save. Until that point, delete, add, and edit changes occur only in a buffer area. Add the following code to the ButtonDelete_Click method.

// buttonDelete code //delete the current row int nRow; nRow = dwc1.CurrentRow; //make sure the user has made a row active by //selecting one with the mouse if (nRow > 0) { dwc1.DeleteRow(nRow); MessageBox.Show(nRow.ToString(), "Deleted a row"); }

One Finishing Touch—Closing the DataWindow Form
Before letting users close the form, you'll want to detect whether they have pending changes (inserts, deletes, edits) that have not been saved. The following code checks for unsaved changes, and when found displays a dialog that provides a warning, but still lets users elect to exit without saving changes. Add the following code to the ButtonClose_Click handler.

// ButtonClose_Click int nRows; dwc1.AcceptText(); nRows = dwc1.ModifiedCount + dwc1.DeletedCount; if (nRows > 0) MessageBox.Show("Save your changes or exit " + "via the window menu",nRows.ToString() + " Pending changes not saved"); else this.Close();

Object-Oriented Design and More
For some final thoughts, it's worth noting that the code you placed on the form is independent of any DataWindow object that you might place on the window. Because the DataWindow separates data access and data interaction from the control, the DataWindow control works with any DataWindow object. To prove this, you'll modify the FirstDataWindow project by adding code that lets you select the DataWindow object that the control uses. Use the dropdown list at the top to select from three different DataWindows, and then click the Retrieve button. You don't have to change a single line of code to get all the functionality of the buttons.

Create two new DataWindows using the DataWindow Designer. Create one DataWindow object named de_stores that accesses the stores table. Create another DataWindow object named dw_sales. Now go back to Visual Studio and add a ComboBox control to the form. Name the object comboBox1. Set the Items property to "Collection". Click the ellipse button, and then enter the following four lines in the string collection editor:

de_authors de_stores dw_Sales de_titlesauthors

Change the code for the buttonRetrieve control so it dynamically changes the DataWindow object based on the combo box selection.

String sDatawindowObject; sDatawindowObject = comboBox1.Text; if (sDatawindowObject.Length <= 3) // set to de_authors if somehow the user did not // select one of the 3 valid datawindows sDatawindowObject = "de_authors"; // tell the datawindow what transation to use along // with the dataobject dw1.DataWindowObject = sDatawindowObject; //dynamically change the datawindow dw1.SetTransaction(transaction1); dw1.Retrieve();

You'll be surprised at the results.

This article has barely scratched the surface of the DataWindow's capabilities as either a user interface control or as a data access control. You can see the query generated when the DataWindow talks to the database by adding the following code to the DataWindow control. Each call to the database (SELECT, INSERT, DELETE, UPDATE) triggers the SqlPreview event. Select the DataWindow control and click the events list to add this event. You'll see question marks in place of the data unless you change the BindVariable parameter.

//dwc1_SqlPreview code MessageBox.Show(e.SqlSyntax,"SQL Generated");

There's simply not enough space in this article to cover more advanced topics, such as:
  • Master-detail windows
  • Drilldown capabilities
  • DropdownDataWindows
  • Validation
  • Dynamic controls
  • Controlling properties using code
  • Datastores (non visual DataWindows)
  • Deployment capabilities
DropdownDataWindows are extremely important when a column in one table relies on a column in another table. In essence, you nest one or more DataWindows within a main DataWindow object to achieve master-detail capability. For example, you can achieve the functionality of embedding a ComboBox within a DataGrid column via the DataWindow with no coding. Set the Edit property to DropDownDW and select the DataWindow you want to use as the dropdown. The sample source code contains two DropdownDataWindows attached to the DataWindow de_titlesauthors.

Figure 28 shows the result of clicking one of the dropdown arrows on a DataWindow containing several DropDownDataWindows.

Figure 27. Embedding a DropDown DataWindow in a Column: Embedding a dropdown list in a column requires no code.
Figure 28. DataWindow with Embedded Dropdowns: The figure shows the result of clicking a dropdown arrow in a DataWindow containing several DropDownDataWindows.
You've seen the most basic DataWindow capabilities, and it should be clear that you can use the DataWindow control to create a form that accesses any database, lets users issue their own queries and return the data in a grid, complete with sort, filter, print, and export functionality—all in fewer than fifty lines of code.

Frank Giannino is a Web Development Manager for a Fortune 100 company, and has worked on managed client/server and Web application development teams for several fortune 500 companies.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date