hen creating a connection to foreign data, the .NET configuration wizard is a great place to start. Not only will it create a point-and-click connection for you, but it also can quickly and easily produce a control for users to retrieve, display, and manipulate the data. However, the wizard seldom will produce a control that provides the exact functionality your users need. You can expect to tweak the wizard's control offerings a bitsometimes quite a bitto add the required functionality.
This article walks the .NET novice coming from Access or SQL Server through building a basic connection and then customizing the resulting control. Using the Visual Basic Express (VB Express) configuration wizard, the article demonstrates how to populate a DataGridView control and then enhance it to do the following:
- Display alternate row colors to create a greenbar affect.
- Disable the DataGridView's built-in sorting capability for a single column.
- Show the edit row when executing the form.
offers a number of ways to retrieve and manipulate foreign data. For someone learning .NET, it's often best to let VB Express do as much of the work as possible. For instance, by simply running its configuration wizard, you can have VB Express establish a connection to the Customers
table in the MS Access sample database, Northwind.mdb
- Launch VB Express and click New Project on the Standard toolbar to start a new project. Click Windows Form Application in the resulting dialog box.
- Enter a meaningful name in the Name control, and click OK.
- Click the Data Sources tab (bottom right) in Solution Explorer. If the tab isn't available, choose Show Data Source from the Data menu.
- Click Add New Data Source to launch the Data Source Configuration Wizard.
- Click Next, as the Database option is the default.
- Click New Connection in the next pane.
- In the resulting Add Connection dialog box, click Change and choose Microsoft Access Database File from the Change Data Source dialog box, and click OK.
- Click Browse in the Add Connection dialog box, locate Northwind.mdb (in the Samples folder of your Office directory), and click OK.
- Click Test Connection and then click OK to clear the confirmation message.
- With the connection working, click OK to return to the wizard, and click Next to continue.
At this point, the wizard needs you to identify specific data.
- VB Express asks you if you want to copy the database. If you click Yes, it will copy the entire database into your project. For this example, click No.
- The next pane lets you save the connection string in the configuration file. The default is Yes, and most of the time this is the best choice. Click Next.
The wizard will retrieve the data objects in the connected database and display them in the source file. This is where you identify the data you want to retrieve and manipulate in your .NET project.
|Figure 1. The Form and Grid in Form Design: Let the Configuration Wizard build a control that can display data.|
- Click the plus sign (+) to expand the Tables node, and check the Customers table. Retain the default name, NorthwindDataSet, that VB Express supplies for the dataset.
- Click Finish, and VB Express will retrieve the specified data.
After creating the connection, you'll need a form to display that data, so do the following:
- Click the Data Sources tab.
- Click the Form1 tab (if necessary) and drag the Customers dataset from the Data Sources tab to Form1 in Form Designer. VB Express will generate a DataGridView control and name it accordingly, using the selected item's name. Figure 1 shows the resulting form and grid in Form Design.
- Choose Save All from the File menu and then click Save.
You now have a dataset filled with customer data from Northwind and a form that displays that data in a DataGridView control. And all you did was run a wizard!