.NET Data Secrets, Part 1, Page 3
Seven Steps to a Quick, Editable Windows Grid
One of the most common requests when dealing with databases is the ability to display a grid that "binds" to a table or view, displaying information and allowing you to quickly edit data. The good news is that in .NET, it's a relatively simple process. The following seven steps will guide you through the exact process.
Ready to rumble?
- Open the Server Explorer (View > Server Explorer). If you're connecting into an SQL Server database, expand the Servers node, locate your target machine (if not available, click the 'Connect to Server' icon and specify the machine name), and then drill down to your database. If you're connecting into another type of database, right-click on the Data Connections node, select Add Connection, and connect into your database.
- Expand the list of tables (or views) and drag the one you want to bind to your grid onto your Windows Form. Two components will be created—a Connection object, which connects into the database, and a DataAdapter object, which acts as the "phone line" between your Connection object and your actual set of data (your "DataSet").
- Right-click the DataAdapter and choose Generate DataSet. A dialog box will appear, about to create the template upon which your DataSet will be based (a DataSet based on a template like this is called a "typed DataSet," whilst the template itself is a customizable "XML schema," sometimes referred to as XSD [XML Schema Definition]). Ensure New is selected; replace the suggested name with something more sensible, such as 'Customer'. Ensure 'Add this DataSet to the designer' is checked. Click OK when finished. Two things will happen: A Customer.xsd (or similar) template will be added to your Solution, and a DataSet component will be added to your form, based on the template. Rename your new DataSet to, say, dsCustomer.
- Drag-and-drop a DataGrid control from the Toolbox onto your Form. Resize as appropriate; then right-click and select Auto Format. Choose a new style, such as "Professional 3" or "Colorful 3".
- Change the DataSource property of your DataGrid control, selecting your DataSet table from the dropdown list.
- Add the following line of code to respond to your form Load event, or place it behind a button. It tells your DataAdapter to talk through the connection, retrieve data, and then pass it into your DataSet (which is feeding the DataGrid):
- Add the following code to respond to your form Closing event, or place it behind a button. It tells your DataAdapter to talk through the connection, updating the source database with any changes made to the DataSet (which may have been through editing the DataGrid):
And that's it. Now all you have to do is run and test!
If you've already created your data objects in code, simply set the DataSource property of your DataGrid to your DataSet table in code; then continue from step six onward.
Top Tip: Don't want a certain column displayed in your Windows Form DataGrid? Simply configure your DataAdapter so it only pulls back the information you want (right-click the DataAdapter and select 'Configure Data Adapter'). Be careful when excluding primary keys, however, because it can cause problems when editing and updating. Alternatively, use views or customize the more flexible ASP.NET DataGrid (see the next "Web Grid" tip). The official Microsoft workaround for 'hiding' columns is more complex; no properties for the columns are exposed directly. However if you're interested, check out http://support.microsoft.com/default.aspx?scid=KB;EN-US;q317951.
Another Top Tip: There are plenty of .NET resources on the Web, but few have spent time collating real Windows DataGrid newsgroup questions quite as well as George Shepherd. Check out his FAQ for answers to common puzzlers at www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp—it's C#-based, but still highly understandable.
My example DataGrid. And it's quite big
Coming up in part two of .NET Data Secrets:
- Nine Steps to a Quick, Editable Web Grid
- Little-Known Technique for Confirming Web Deletes
- Selecting Multiple Web Form Grid Items, Hotmail-Style
- Click Anywhere and Select, with a Web Grid
See you next time!
About the Author
Karl Moore is a technology author living in Yorkshire, England. He runs his own consultancy group, White Cliff Computing Ltd, and is author of two best-selling books exposing the secrets behind Visual Basic .NET. When he's not writing for magazines, speaking at conferences, or making embarrassing mistakes on live radio, Karl enjoys a complete lack of a social life.
All of these tips are quick and nifty versions of those you'll find in Karl's newest book, Ultimate VB .NET and ASP.NET Code Book.
# # #