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 3

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
Sorting and Filtering
While the BindingNavigator control allows you to navigate records by using the VCR-style controls, it is not a practical solution when you have a large number of records. It would be much better to have a list of records that users can select directly.

 
Figure 12. Making Changes: Here's how to modify the form to add the extra ListBox and Button controls.
Add Sorting Capability
To enhance the application, add two ListBox controls and two Button controls onto the form (Figure 12).

ListBox1 and ListBox2 will respectively be used to display the first and last names of the authors. Button1 allows you to sort the authors by first name while Button2 sorts the authors by last name. In the smart tag of the ListBox1 control, select the AuthorsBindingSource as the DataSource property, au_fname as DisplayMember, and au_id as the ValueMember property (see Figure 13). Do likewise for ListBox2, except that its DisplayMember should be set to au_lname.

Double-click on Button1 and add the following code.

Private Sub Button1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click AuthorsBindingSource.Sort = "au_fname" End Sub

 
Figure 13. Choosing a Binding Source: Bind the ListBox control to the appropriate BindingSource control.
Essentially, you are sorting the authors by the author's first name. Likewise, add this code to Button2 so you can sort by last name.

Private Sub Button2_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button2.Click AuthorsBindingSource.Sort = "au_lname" End Sub

To test the application, press F5. You can now either select an author's first name or last name and the corresponding author's detail will be shown in the controls on the right (see Figure 14).

 
Figure 14: Another Test: Here's how the application should look when you run it.
 
Figure 15. Sorting: You can sort records by first or last name.
You can also sort the names by either first name or last name (see Figure 15).

 
Figure 16: Modifications: Here's Form1 after adding the new controls.
Add Filtering Capability
Besides sorting records, you can also filter them. This is useful if you want to search for certain records.

In Form1, add the controls shown in Figure 16.

Double-click on Button3 and code the following.



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

 
Figure 17. Searching: After making the modifications, you can search for a particular record.
If TextBox1.Text = "" Then AuthorsBindingSource.RemoveFilter() Else AuthorsBindingSource.Filter = _ "au_id LIKE '*" & _ TextBox1.Text & "*'" End If End Sub
The above code searches for records based on au_id. If the search string is empty, the filter will be removed and all records will be displayed. To test the application, press F5. Enter some numbers and all records with au_id field containing the search string will be displayed (see Figure 17).



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap