Browse DevX
Sign up for e-mail newsletters from DevX


ADO.NET: Building Your First Data-Aware Form : Page 3

The first article in this series detailed how to build data-aware forms in Visual Studio .NET using the Data Form Wizard. It demonstrated how easy it is to build forms using a point-and-click interface. You also learned that the generated form was not especially suited to production development. This article continues where that article left off and introduces you to techniques to improve data-aware forms created with the Data Form Wizard.




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

The Improved Data Entry Form
Figure 2: The improved version of the Authors Data Entry Form.
Figure 2 illustrates the new and improved version of the Authors Data Entry Form. This version uses a drop-down combo box to navigate through the authors. In addition, this version toggles the enabled status of various controls based on whether the user is in add, edit or view mode. The inner workings of the data form are actually quite simple. The following sections take you through the different portions of the code to give you a lay of the land.

Initializing the Form A form property called AuthorData is created to hold a reference to the Data Tier Component discussed earlier. The form also has a property to hold a reference to a DataSet that holds the current author information. The DataSet is produced from the Data Tier Component. Finally, there is a form-level property to keep track of the current edit mode status. This data type is an enumerated type that can have the value of add, edit or view.

In the New procedure, as shown in Listing 11, the author drop-down combo box is populated, the mode is set to view and the ChangeMode Method is invoked. The ChangeMode is a form-level method that simply toggles the enabled status of the various form objects. That method is discussed in a few moments. PopulateAuthorCombo Method

The PopulateAuthorCombo method, as shown in Listing 12, invokes the Data Component's GetList method, which returns a DataSet. With a few lines of code, the combo box can bind to the dataset. It is a matter of setting the DataSource, DisplayMember and ValueMember properties for the combo box. The only additional work involves saving the combo box's SelectedIndex property and restoring it, if necessary. ChangeMode Method

The ChangeMode method, as shown in Listing 13, toggles the enabled status of a control based on the current mode (add, edit or view). Every time the user initiates an operation, the mode property is updated and a call is made to the ChangeMode method. In further illustrations, you'll see numerous calls to the ChangeMode method. DisplayData Method

A call is made to the DisplayData method whenever the selected value of the combo box changes. The DisplayData method, as shown in Listing 14, gets the primary key information from the selected item in the combo box, and then passes that information to the Data Tier Component's GetData method. If you recall, the GetData method returns a DataSet that contains data of the selected author. The last step is a call to the CreateBindings method, which is discussed in the next section. CreateBindings Method

Form elements like a textbox, combo box, checkbox, etc. can bind to elements in a DataSet. Specifically, a textbox can bind to a field which is contained in a table which in turn is contained in a DataSet. Each form element can support one active binding at a time. That is why the bindings are preemptively cleared at the beginning of the CreateBindings method, shown in Listing 15. Also, in order to refresh the data on the form, this method of clearing and re-establishing the bindings proved to be the easiest and most straightforward way of accomplishing the task. Once the bindings are cleared, you add a data binding to each object by specifying the object's property to display the data. For a textbox, the Text property is used to display and update the data. For a checkbox, the Selected property is used for data binding. You could specify the Text property, but then you would see the label text change and you would have no way of writing data back! In each case, you need to specify which DataSet as well as the table and field the data is located. As you'll see, it works like a charm! WriteData and DeleteData Methods

When you click a command button, it invokes one of the form level methods. To review, whenever you pick an item from the Author combo box, the DisplayData method is invoked, which does the work of getting data from the Data Tier Component and binding the data to the various form controls. When you click the Save button, the WriteData method, as shown in Listing 16, is invoked. The Mode property determines whether the Data Tier Component's InsertData or UpdateData method is invoked. Regardless of which method is invoked, the DataSet holding the current and perhaps modified or new author information is passed as a parameter to the respective method. Once the data has been updated, a call is made to the PopulateAuthorCombo and DisplayData methods. Finally, the mode is reset to view, toggling the enabled status of the various form controls. The DeleteData method, as shown in Listing 17, is fairly straightforward. The user is prompted to confirm whether the data should be deleted and, if the user answers yes, the Data Tier Component's DeleteData Method is invoked. Again, the PopulateAuthorCombo and DisplayData methods are invoked. This Delete button is only enabled in view mode, so there is no need to call the ChangeMode method.

What About Closing the Form with Pending Changes? In earlier versions of Visual Basic, forms had a QueryUnload event that you could use to trap when the user pressed the Close box on the form toolbar. Like many things, there is a different way of doing the same thing in .NET. Windows forms support a Closing event that enables you to trap when user clicks the Close button. As seen in Listing 18, the form's Mode property is checked and if it is not View, the user is prompted to save or cancel their changes first. The code looks quite a bit different than the equivalent Visual Basic 6 code, but it accomplishes the same job.

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