Using Data Sources with the DataGridView Control - How to generate a DataGridView control from a data source
(Page 2 of 5 )
By default, if you drag a table from the Data Sources window onto a form, Visual Studio adds a DataGridView control to the form and binds it to the table as shown in figure 14-6. This creates a DataGridView control that lets you browse all the rows in the table as well as add, update, and delete rows in the table. To provide this functionality, Visual Studio adds a toolbar to the top of the form that provides navigation buttons along with Add, Delete, and Save buttons.
To bind a DataGridView control to a table, Visual Studio uses a technique called complex data binding. This just means that the bound control is bound to more than one data element. The DataGridView control in this figure, for example, is bound to all the rows and columns in the Products table.
When you generate a DataGridView control from a data source, Visual Studio also adds four additional objects to the Component Designer tray at the bottom of the Form Designer. First, the DataSet object defines the dataset that contains the Products table. Second, the TableAdapter object provides commands that can be used to work with the Products table in the database. Third, the BindingSource object specifies the data source (the Products table) that the controls are bound to, and it provides functionality for working with the data source. Finally, the BindingNavigator defines the toolbar that contains the controls for working with the data source.
Before I go on, I want to point out that the TableAdapter object is similar to the DataAdapter object you learned about in the previous chapter. However, it can only be created by a designer. In addition, it has a built-in connection and, as you’ll see later in this chapter, it can contain more than one query.
I also want to mention that, in general, you shouldn’t have any trouble figuring out how to use the binding navigator toolbar. However, you may want to know that if you click the Add button to add a new row and then decide you don’t want to do that, you can click the Delete button to delete the new row. However, there’s no way to cancel out of an edit operation. Because of that, you may want to add a button to the toolbar that provides this function. You can learn how to add buttons to a toolbar in the next chapter.
A form after the Products table has been dragged onto it

The controls and objects that are created when you drag a data source to a form
Control/object | Description |
DataGridView control | Displays the data from the data source in a grid. |
BindingNavigator control | Defines the toolbar that can be used to navigate, add, update, and delete rows in the DataGridView control. |
BindingSource object | Identifies the data source that the controls on the form are bound to and provides functionality for working with the data source. |
DataSet object | Provides access to all of the tables, views, stored procedures, and functions that are available to the project. |
TableAdapter object | Provides the commands that are used to read and write data to and from the specified table in the database. |
Description
- To bind a DataGridView control to a table in a dataset, just drag the table from the Data Sources window onto the form. Then, Visual Studio automatically adds a DataGridView control to the form along with the other controls and objects it needs to work properly. Because the DataGridView control is bound to the table, it can be referred to as a bound control.
- To bind a DataGridView control to a data table, Visual Studio uses a technique called complex data binding. This means that the control is bound to more than one data element, in this case, all the rows and columns in the table.
Figure 14-6. How to generate a DataGridView control from a data source
Next: How to edit the properties of a DataGridView control >>
More Visual Basic.NET Articles
More By Murach Publishing
|
This article is excerpted from chapter 14 of the book Murach's Visual Basic 2005, written by Anne Boehm (Murach, 2006; ISBN: 1890774383). Check it out today at your favorite bookstore. Buy this book now.
|
|