|
Manage Data With VS 2005 (Continued)
DataSet updates dictate that most DataGridViews be data-bound to relational tables, but you also can write code to add and populate rows of unbound DataGridViews with SqlCommands that execute SqlDataReaders. Few developers will miss the DataGridView's lack of support for the obsolete DataGrid's cumbersome hierarchical data display. DataGridViews sport an almost absurdly granular event repertoire; the controls' 149 events threaten to induce an event choice crisis. A combination of bound textboxes, other simple-bound controls, and DataGridViews probably will handle 90 percent or more of day-to-day data display and editing requirements. But the DataGridView certainly won't destroy the market for custom third-party grids and other data-bound controls with more extensive feature sets.
Many familiar Windows Forms controls support ex post facto databinding for an existing UI design. For example, you can drag a table column icon from the Data Sources window and drop it on an existing TextBox, CheckBox, RadioButton, NumericUpDown button, DateTimePicker, or MonthCalendar to bind the control's value. (DateTimePicker and MonthCalendar controls still don't handle null values gracefully.) DropDownList and ListBox controls have a Smart Task tag with a checkbox to enable databinding; marking the checkbox lets you select a Data Source, Display Member, Value Member, and Selected Value. The Windows Forms ReportViewer renders SQL Server Reporting Services report (RDLC) files you create with the ReportDesigner. Microsoft added the ReportViewer and ReportDesigner to VS 2005, VB and C# Express, and Visual Web Developer editions at the beta 2 stage.
Projectless ASP.NET 2.0 Web sites receive a complete data-bound control rework. Web sites don't have a Data Sources window, but—unlike Smart Client projects—you can drag a table node from a Server Explorer database connection to a page in Design view. Dropping a table's node on the page adds an SqlDataSource1 placeholder and a default read-only GridView control bound to the table. ASP.NET 2.0's version of the Configure Data Source Wizard is almost identical to that for Windows Forms. SQL Server is the default data source, as you'd expect, but you can specify Oracle, Access (Jet), or other OLE DB or ODBC data sources, each of which implements two-way binding for controls that derive from the DataBoundControl class. Alternatively, you can add a SqlDataSource, AccessDataSource, ObjectDataSource, XmlDataSource, or SiteMapDataSource from the ToolBox's Data section, and then configure it with the Configure Data Source Wizard.
The new GridView control, which replaces ASP.NET 1.x's DataGrid, offers declarative formatting, sorting, editing, deleting, and paging capabilities. The Auto Format Smart Task lets you apply one of 17 preset color schemes. Enable the behaviors you want by marking Smart Task checkboxes. Notice that GridView controls don't support inserting new rows (see Figure 4). The Delete command button isn't enabled for the sample Customers GridView because the Orders table's foreign-key constraint would throw an unhandled exception when attempting to delete a Customers record. In contrast to working with the DataGrid, you don't need to write any code to create an easy-to-use data-retrieval and editing page. However, the inability to execute INSERT commands from GridView controls is a serious limitation, and workarounds that use templated textboxes in footer columns are cumbersome at best. Use the new DetailsView or FormView control to add rows to the underlying table.
The Web site's UI lacks a Data Sources window with its hierarchical view of related tables to create codeless master-details forms. This omission means that you must add parameterized WHERE clause constraints to SqlDataSources so data-bound server controls display related record(s). When you add a WHERE ColumnName = @ColumnName clause to the SqlDataSource's SELECT statement, the Configure Data Source Wizard opens a Define Parameters dialog that lets you select the source for one or more parameter values. Parameter value sources include cookies, control values, form fields, profile properties, query strings, and session fields.
If you select QueryString as the parameter's data source, you can replace a GridView's Select command button with a HyperLinkField that has its DataNavigateUrlFields property value set to ColumnName and DataNavigateUrlFormatString set to WebForm.aspx?columnname={0}. The selected ColumnName value replaces the {0} placeholder. For example, clicking on the sample DataWebSite1 Web site's Select link for Rattlesnake Canyon Grocery on the Default.aspx page posts back with http://localhost:15739/DataWebSite1/DetailsView.aspx?customerid=RATTC as the URL. The customerid=RATTC query string sets the DetailsView page's CustomerByCustomerIDParam SqlDataSource to the appropriate row for editing in the CustomersDetailsView control. Similarly, a DataNavigateUrlFields property value of CustomerID, OrderID, and DataNavigateUrlFormatString value of DetailsView.aspx?orderid={0}&customerid={1} return http://localhost:15739/DataWebSite1/DetailsView.aspx?orderid=11077&customerid=RATTC as the post-back URL to populate OrderDetailsByOrderIDParam data source and its bound OrderDetailsGridView control (see Figure 5). Populating the DataWebSite1 application's four data-bound controls and editing the three tables don't require a single line of code.
GridView and DetailsView controls have optional Footer, Empty Data, and Pager templates; DetailsView adds an optional Header template. The Fields dialog has a link that lets you convert individual bound fields to template fields. FormView controls provide greater formatting flexibility by providing default Item, EditItem, and InsertItem templates for each field. The EditItem and InsertItem templates let you substitute other data-bound Web controls—such as DropDownLists (see Figure 6) or CheckBoxes—for textboxes. ASP.NET 2.0 DataLists and Repeater controls are quite similar to their 1.x counterparts. The ASP.NET ReportViewer is a sample server control that's intended for URL-based navigation and rendering of reports in Internet Explorer instead of a Smart Client form.
Test-drive the SmartClient1 project and then review the code behind the Northwind.vb form. A simple database front end that doesn't need to update back-end tables doesn't require writing a single line of code. Autogenerated code for the Northwind_Load event handler fills the three primary and two lookup tables. Updating more than one table requires adding only two instructions per table, as illustrated by the bindingNavigatorSaveItem_Click event handler's code. The DataWebSite1 project's two forms don't require any added code. Although real-world projects certainly will require .NET 2.0 developers to write some VB or C# code, I believe you'll agree that VS 2005 is as close as you're likely to come to the nirvana of a truly codeless presentation layer for data-intensive .NET applications.
 |
About the Author
Roger Jennings is an independent XML Web service and database developer and author. Roger's latest book is Expert One-on-One Visual Basic 2005 Database Programming (Wrox). He's also a Visual Studio Magazine contributing editor and FTPOnline columnist, and he manages the OakLeaf Systems XML Web Services demonstration site and Weblog. His Code of Federal Regulations Web services won Microsoft's 2002 .NET Best Horizontal Solution Award. Reach Roger at .
Back to top
|