Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Data Binding in Windows Forms 2.0 : Page 4

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.




Application Security Testing: An Integral Part of DevOps

Displaying Master-Details Relationships
Another common scenario in data binding is displaying master-details relationships. For example, in the pubs database, several tables are related. The authors table is related to the titles table through the titleauthor table. For example, using the above example, if a particular author is selected, it might be useful to display the title of all books written by that author.

To do so, let's add two more tables to the pubsDataSet object. In the Data Sources window, right-click on pubsDataSet and select the Configure DataSet with Wizard… item (see Figure 18).

Figure 18. DataSet Configuration: The figure shows the process of configuring the dataset.
Figure 19. Table Relationships: You can see the relationship between the three tables in this figure.
Figure 20. Customizing Binding: Here's how you customize the binding for a table.
In the "Choose your database objects" dialog box, choose the titleauthor and titles tables. The pubsDataSet object should now contain three tables: authors, titleauthor, and titles (Figure 19).

To bind the titleauthor table to a ListBox control, you need to change its binding in the Data Source window. Click on the drop-down list for the titleauthor item and select "Customize…" (see Figure 20).

Under the "Associated controls" section (see Figure 21), check the ListBox checkbox, and click OK.

Figure 21. Associated Controls: The figure shows where to select the ListBox checkbox.
Figure 22. Binding Tables: The figure shows the completed bindings for the two tables.
Figure 23. Updated Form: Here's the updated sample form.
In the Data Sources window, ensure that the titleauthor object is bound to the ListBox and the titles object is bound to Details (Figure 22).

Expand the width of Form1 and drag and drop the titleauthor and titles objects onto the form. Figure 23 shows how the form looks after these changes. Now add two Label controls onto the form and set their Text properties as shown.

You should also observe that there are four new objects created after the drag-and-drop operation (see Figure 24).

Figure 24. More Controls: Adding the bindings adds four new controls.
Figure 25. Setting Properties: Setting the Value Member of the ListBox control to title_id.
In the smart tag of the ListBox control, set its ValueMember to title_id (see Figure 25).

In the code-behind of Form1, add the following subroutine.

Public Sub updateTitles() TitlesBindingSource.Filter = _ "title_id='" & TitleauthorListBox. _ SelectedValue & "'" End Sub

The updateTitles() subroutine will apply a filter to the TitlesBindingSource control (which is bound to the various controls on the right of the form, showing the details of a book) so that only titles with title_id matching the title_id selected in the TitleauthorListBox control are shown.

Double-click on the ListBox1 control so that when an author's first name is selected, a filter is applied to the TitleauthorBindingSource control to display all titles authored by the selected author in the TitleauthorListBox control.

Private Sub ListBox1_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ListBox1.SelectedIndexChanged TitleauthorBindingSource.Filter = _ "au_id='" & _ ListBox1.SelectedValue & "'" updateTitles() End Sub

Note that you only need to implement the SelectedIndexChanged event for either ListBox1 or ListBox2.

Likewise, double-click the TitleauthorListBox control and code the following.

Private Sub TitleauthorListBox_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles TitleauthorListBox.SelectedIndexChanged

Figure 26. Final Test: Here's how the application should look after completing all the modifications.
updateTitles() End Sub
The above code ensures that when the items in the TitleauthorListBox control are refreshed, the detailed book information of the first title is displayed.

To test the application, press F5. Figure 26 shows the flow of the application.

In this article, you have seen how data binding works in Windows Forms 2.0. There is no need for you to write lots of code in order to perform some common tasks. You just need to know the function of each control (such as BindingSource) and configure them accordingly.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
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