Introduction
The DataGridView control available as a part of Windows Forms controls in Visual Studio 2008 is much more powerful than it's previous versions. This tutorial is a basic introduction of how to write a simple data driven application using Visual Studio 2008 wizards without writing a single line of code. In my next articles, I will talk about more detailed features of the DataGridView control.
The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data. This control also let's you display data in a master-details view.
With the DataGridView control, you can display and edit tabular data from many different kinds of data sources. Binding data to the DataGridView control is straightforward and intuitive, and in many cases it is as simple as setting the DataSource property. When you bind to a data source that contains multiple lists or tables, set the DataMember property to a string that specifies the list or table to bind to.
The DataGridView control supports the standard Windows Forms data binding model, so it will bind to instances of classes described in the following list:
- Any class that implements the IList interface, including one-dimensional arrays.
- Any class that implements the IListSource interface, such as the DataTable and DataSet classes.
- Any class that implements the IBindingList interface, such as the BindingList<(Of <(T>)>) class.
- Any class that implements the IBindingListView interface, such as the BindingSource class.
The DataGridView control supports data binding to the public properties of the objects returned by these interfaces or to the properties collection returned by an ICustomTypeDescriptor interface, if implemented on the returned objects. Typically, you will bind to a BindingSource component and bind the BindingSource component to another data source or populate it with business objects. The BindingSource component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically.
The DataGridView control can also be used in unbound mode, with no underlying data store. For a code example that uses an unbound DataGridView control, see Walkthrough: Creating an Unbound Windows Forms DataGridView Control. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. When you want your Windows Forms application to display tabular data, consider using the DataGridView control before others (for example, DataGrid). If you are displaying a small grid of read-only values, or if you are enabling a user to edit a table with millions of records, the DataGridView control will provide you with a readily programmable, memory-efficient solution.
Getting Started
OK let's get started. Follow these steps and you will see how to write a data driven application without writing a single line of code.
Step 1. Create a new Windows Forms Application project in Visual Studio 2008.
Figure 1.
Step 2. Drag and drop DataGridView control from toolbox to form window.
Figure 2.
Step 3. Now choose a data source by right clicking on the DataGridView and then click on Add Project Data Source. We will be adding a new data source to the project right now.
Figure 3.
Step 4. Now select data source type, I am choosing Database in this example.
After selecting Database, click next.
Figure 4
Step 5. Choose your data connection, if you already have a connection available select that, otherwise make new connection, and follow the steps and after that click next.
Figure 5.
Step 6. Add Connection Wizard.
Figure 6
Step 7. Choose you database objects, and click next, if you want local database caching then check enable local database caching check box and click next.
Figure 7.
Step 8. Select table for caching.
Figure 8.
Step 9. Now your data source is created, right click on DataGridView control and on the option Choose Data Source, select the data source you just created.
Figure 9.
That's all. You are all set.
Now you simply need to add one line of code. If your form load event does not have this code allready. add this code. Otherwise it should have it.
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'vendorDataSet.Vendor' table.
// You can move, or remove it, as needed.
this.vendorTableAdapter.Fill(this.vendorDataSet.Vendor);
}
Step 10. Now build and run the application. Your application looks like the following.
Figure 10.
Summary
In this tutorial, you saw how to build a simple data driven application using a DataGridView control in Visual Studio 2008 without writing a single line of code.
More articles on DataGridView