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 placing a DataGridView control on a form. It also creates about two dozen other controls and components. Four of the more important of these objects are the DataSet, TableAdapter, BindingSource, and BindingNavigator. You can see these components in the component area below the form in Figure 11-17.

image from book
Figure 11-17: Visual Basic uses DataSet, TableAdapter, BindingSource, and BindingNavigator components to manage a DataGridView display.

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 items 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 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 11-18 shows the relationships among the DataSet, TableAdapter, BindingSource, and BindingNavigator objects.

image from book
Figure 11-18: Visual Basic uses DataSet, TableAdapter, BindingSource, and BindingNavigator objects to display data. BindingSource

Even all these objects working together 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. The Form1_Load event handler makes the TableAdapter copy data from the database into the DataSet.

The bindingNavigatorSaveItem_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 to the database.

  Private Sub bindingNavigatorSaveItem_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles bindingNavigatorSaveItem.Click     If Me.Validate Then         Me.StudentsBindingSource.EndEdit()         Me.StudentsTableAdapter.Update(Me.ClassRecordsDataSet.Students)     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 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 'ClassRecordsDataSet.     ' table. You can move, or remove it, as needed.     Me.StudentsTableAdapter.Fill(Me.ClassRecordsDataSet.Students) End Sub  

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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net