AUTOMATICALLY CREATED OBJECTS

When you drag database tables and columns from the Data Sources window onto a form, Visual Basic does a lot more than simply place a DataGridView control on a form. It also creates about two dozen other controls and components. Five of the more important of these objects are the DataSet, TableAdapter, TableAdapterManager, BindingSource, and BindingNavigator.

The program stores data in a DataSet object. A single DataSet object can represent an entire database. It contains DataTable objects that represent database tables. Each DataTable contains DataRow objects that represent rows in a table, and each DataRow contains DataColumn objects representing column values for the row.

The TableAdapter object copies data between the database and the DataSet. It has methods for performing operations on the database such as selecting, inserting, updating, and deleting records. Hidden inside the TableAdapter is a connection object that contains information on the database so that the TableAdapter knows where to find it.

The TableAdapterManager coordinates updates among different TableAdapters. This is most useful for hierarchical data sets, a topic that is outside the scope of this book. The wizard-generated code also uses the TableAdapterManager to update the single data set it creates.

The BindingSource object encapsulates all of the DataSet object’s data and provides programmatic control functions. These perform such actions as moving through the data, adding and deleting items, and so forth.

The BindingNavigator provides a user interface so the user can control the BindingSource.

Figure 19-9 shows the relationships among the DataSet, TableAdapter, BindingSource, and BindingNavigator objects. The BindingNavigator is the only one of these components that has a presence on the form. It is connected to the BindingSource with a dotted arrow to indicate that it controls the BindingSource but does not actually transfer data back and forth. The other arrows represent data moving between objects.

FIGURE 19-9: Visual Basic uses TableAdapter, DataSet, BindingSource, and BindingNavigator objects to display data.

image

Even working together, all these objects don’t quite do everything you need to make the program display data. When it creates these objects, Visual Basic also adds the following code to the form:

public Class Form1
    Private Sub StudentsBindingNavigatorSaveItem_Click(
    sender As Object, e As EventArgs) _
    Handles StudentsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.StudentsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ClassRecordsDataSet)
    End Sub
        
    Private Sub Form1_Load(sender As Object, e As EventArgs) _
    Handles MyBase.Load
        'TODO: This line of code loads data into the 'ClassRecordsDataSet.Students'
        ' table. You can move, or remove it, as needed.
        Me.StudentsTableAdapter.Fill(Me.ClassRecordsDataSet.Students)
    End Sub
End Class

The StudentsBindingNavigatorSaveItem_Click event handler fires when the user clicks the BindingNavigator object’s Save tool. This routine makes the TableAdapter save any changes to the Students table in the database.

The Form1_Load event handler makes the TableAdapter copy data from the database into the DataSet when the form loads.

Visual Basic builds all this automatically, and if you ran the program at this point, it would display data and let you manipulate it. It’s still not perfect, however. It doesn’t perform any data validation, and it will let you close the application without saving any changes you have made to the data. It’s a pretty good start for such a small amount of work, however.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.250.153