Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Data Binding in Windows Forms 2.0 : Page 2

Windows Forms 2.0 increases support for data binding via the new BindingNavigator and BindingSource objects, which will save you a lot of effort. Find out how you can perform sorting and searching tasks using data binding and simplify the display of master-detail relationships in tables.


advertisement
Data Binding-related Controls and Usage
Here are the uses of the various controls:

  • PubsDataSet—A dataset used to represent the tables and relationships in the pubs database.
  • AuthorsBindingSource—The component that binds your controls (in this case the DataGridView) to the data sources.
  • AuthorsTableAdapter—Used to fill the dataset (in this case the PubsDataSet) with records from the data sources.
  • AuthorsBindingNavigator—sed for navigating records during runtime using the VCR-style navigation buttons.
When the form is loaded, the AuthorsTableAdapter is used to fill the PubsDataSet object (the code below is automatically generated by Visual Studio 2005; there is no need for you to write it).

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load 'TODO: This line of code loads ' data into the ' PubsDataSet1.titles' ' table. You can move, or remove ' it, as needed. Me.AuthorsTableAdapter.Fill(_ Me.PubsDataSet.authors) End Sub

 
Figure 8. Testing: Testing the application.
To test the application, press F5. You should see the records in the authors table displayed in the DataGridView control (see Figure 8). Use the navigational buttons to move between records. To add a new record, click on the Add New button and enter the details for the new record. To save the changes to the database, click the Save Data button. Likewise, to delete a record, select a row and then click the Delete button and then the Save Data button to persist the changes to the database. To edit a row, make the changes in the required cell and then click the Save Data button.

How does the BindingNavigator add, edit, and delete the records? Actually, the code is all written for you in the Save Data button Click event handler as shown below:

Private Sub bindingNavigatorSaveItem_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles bindingNavigatorSaveItem.Click

 
Figure 9. Changing Control Type: Here's how you change the type of control to which you want to bind.
If Me.Validate Then Me.AuthorsBindingSource.EndEdit() Me.AuthorsTableAdapter.Update( _ Me.PubsDataSet.authors) Else System.Windows.Forms.MessageBox.Show( _ Me, "Validation " & "errors occurred.", _ "Save", System.Windows.Forms. _ MessageBoxButtons.OK, _ System.Windows.Forms. MessageBoxIcon.Warning) End If End Sub
Changing Bindings
By default, the authors table is bound to a DataGridView control. You can change it to bind to some other controls. In the Data Sources window, click on the drop-down ListBox for authors and select "Details" (see Figure 9). Also, change the binding for the au_id field to a Label control.

Remove all of the controls on Form1, and drag and drop the authors table (in the Data Sources window) onto it. Instead of the DataGridView control, you will now see several Label and TextBox controls (see Figure 10). The text in the Label controls are based on the field names in the table. For example, the au_id field is interpreted as "au id". Visual Studio 2005 is smart enough to guess the appropriate name to use for the Label controls. (Of course, in this example the label texts are less than ideal, but if you have a field name such as employee_name, it works out pretty well). Also, note that a Label control is used for the value of the au_id field, while the other fields use TextBox controls. This is based on the changes that you have just made.

 
Figure 10. Binding to Labels and TextBoxes: The figure shows how to use Label and TextBox controls for databinding.
 
Figure 11. Testing: Here's the application in action.
Press F5 to test the application. You can now view individual records by using the BindingNavigator control (see Figure 11).



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap