Simple Binding Complex Binding Binding Data to Controls Navigating in Datasets Using the DisplayMember and ValueMember Properties Creating Data Forms with the Data Form Wizard Using SQL Parameters Immediate Solutions: Using the DataBindings Property for Data Binding Using the ControlBindingsCollection Class Using the Binding Class Creating Simple Data Binding Creating Complex Data Binding Binding Text Boxes Binding Buttons Binding Checkboxes Binding Radio Buttons Binding Combo Boxes Binding List Boxes Binding Checked List Boxes Using the DisplayMember and ValueMember Properties Using the DataGrid Class Binding Data Grids Using the BindingContext Class Navigating in Datasets Creating Data Forms Automatically Using Parameterized SQL Queries Using Master/Detail Relationships and Data Relation Objects Using the ErrorProvider Class Performing Data Validation in Controls
In this chapter, we're going to take a look at binding controls to data sources. This usually means binding controls to data from databases; for example, you might bind a text box to the last names of authors (that is, the au_lname field) from the authors table of the pubs database, which would make the text box display those names automatically as you moved through the database. Or you might bind the whole authors table to a data grid, as we did in the previous chapter. However, data binding has gone far beyond the traditional. In Windows forms, you can now bind controls not to just data in databases, but to just about any programming construct that holds data. For example, you can bind control properties to an array of values, the data you read from a file, or to the properties of another control.
You also can now bind any property of any control to a data source. For example, not only can you bind the Text property of a text box to a data source, but also the size and image in a picture box, the background color of a label, even whether or not a list box has a border. In Visual Basic .NET, data binding has become an automatic way of setting any property that you can access at run time of any control in a form.
There are plenty of ways to use data binding in Visual Basic applications; here are some common scenarios:
Navigation—When you bind a data source to controls, you can display the data in that source and allow the user to move through that data, record by record. This is a great way to give the user easy access to your data.
Data Entry—Using data binding, you can create data-entry forms, letting the user enter data that is then sent to a database. The user can enter data using, for example, text boxes, radio buttons, list boxes, drop-down list boxes, and checkboxes, making it easy to work with what otherwise might be a complex database system.
Master/Detail Applications—When you have a data relation that ties tables together, binding that data to controls can let you make use of that relation. For example, you may display the names of publishers from the publishers table in the pubs database in a combo box, and, when the user selects one of the publishers, the titles they've published, from the table named titles, come up in a data grid. (The connection between the two tables is supported with the pub_id field; see "Using Master/Detail Relationships and Data Relation Objects" in this chapter for an example of this at work.) This is called a master/detail, or parent/child, relation between the tables.
Data Lookups—Your code may deal with product ID and SKU codes, but you may want to display the actual names of the products you're dealing with. For example, it's going to be a lot easier to understand "Lawn Tennis Kit" than ID 438583920, and using data binding, you can display a user-friendly name like "Lawn Tennis Kit" while at the same time working with the associated code-friendly values like 438583920 behind the scenes in your program. This is supported with the ValueMember and DisplayMember properties that controls such as list boxes support; when the user chooses their lawn tennis kit, your code can actually read 438583920 from the list box.
We're going to see how all of these techniques work in this chapter, with examples. Before we start actually doing some data binding, we first have to understand that there are two ways of binding data in Visual Basic—simple and complex. And there are advantages and disadvantages to both these techniques.