Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The .NET Newbie's Guide to Customizing the DataGridView Control

If you're a .NET newbie coming from Access or SQL Server, let .NET do the hard work of creating a connection to foreign data. Then customize the resulting control to add functionality.


advertisement
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 bit—sometimes quite a bit—to 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.

Getting Started

VB Express 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:
  1. 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.
  2. Enter a meaningful name in the Name control, and click OK.
  3. Click the Data Sources tab (bottom right) in Solution Explorer. If the tab isn't available, choose Show Data Source from the Data menu.
  4. Click Add New Data Source to launch the Data Source Configuration Wizard.
  5. Click Next, as the Database option is the default.
  6. Click New Connection in the next pane.
  7. 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.
  8. Click Browse in the Add Connection dialog box, locate Northwind.mdb (in the Samples folder of your Office directory), and click OK.
  9. Click Test Connection and then click OK to clear the confirmation message.
  10. With the connection working, click OK to return to the wizard, and click Next to continue.
  11. At this point, the wizard needs you to identify specific data.
  12. 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.
  13. 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.
  14. Figure 1. The Form and Grid in Form Design: Let the Configuration Wizard build a control that can display data.
    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.
  15. 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.
  16. 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:

  1. Click the Data Sources tab.
  2. 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.
  3. 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!



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap