Section 5.2. Create Detail Pages


5.2. Create Detail Pages

Visual Studio 2005 provides a number of sophisticated, new controls that make it easy for you to interact with data without getting your hands dirty with ADO.NET plumbing. New wizards are available for presenting data in sophisticated ways without writing code.


Note: Visual Studio 2005 provides a number of sophisticated new controls that allow you to create flexible datacentric applications without delving into ADO.NET plumbing.

A classic data requirement is to present the data from a table (for example, the Northwind Orders table) on a form, with text boxes and other controls for interacting with that data. You can do so with virtually no code using the new data-bound controls.

5.2.1. How do I do that?

To begin, create a new Windows application named OrderDetails. Before adding anything to the form, create a DataSet with a single table based on the Orders table in the Northwind database. To do so, start with the menu choice Data Show Data Sources.

Click the Add New Data Source link to open the Data Source Configuration Wizard. You can bind your data to a number of different types of data sources, but in this case you want to bind to a database. Click Next, and use the existing connection created in the previous lab (or create a new connection to the Northwind database now).

Click the Orders table in the Data Sources view, and an arrow appears. You can tell the Orders data source that you want to display the data as a DataGridView (as you did in the previous lab) or that you want to display the details. Select Details, as shown in Figure 5-11.

Figure 5-11. Choosing Details in the Orders table


The drop down will close. Drag the Orders table from the Data Sources window onto your form. Labels and data entry controls are created for you, one for each selected field in the Orders table, as shown in Figure 5-12.

Figure 5-12. The form populated for the Details view


5.2.2. What just happened?

In addition to populating your form, a number of controls are added to your tray, including OrdersTableAdapter, OrdersBindingNavigator, NorthwindDataSet, and OrdersBindingSource.

Table adapters are designer-generated components that connect your DataSet to the underlying data source. Table adapters are similar to data adapters, but they are strongly typed and can contain multiple queries to support multiple tables.

Binding navigators are a standardized, type-safe way to navigate through and manipulate your control. OrdersBindingNavigator is manifested as a toolbar at the top of the form. The BindingSource component simplifies the binding of controls to an underlying data source. It provides an abstraction of the form's data connection, and it passes commands to the underlying data source. BindingNavigator and BindingSource work together as intermediaries between the DataSet and the form's controls.

Open the frmOrderDetails.cs file and you'll find that the wizard has added one line of code to the Form_Load event handler:

private void Form1_Load(object sender, EventArgs e) {    this.ordersTableAdapter.Fill(this.northwindDataSet.Orders);      }

TableAdapter is filled with the contents of the Orders table. All the rest is automatic from there.

5.2.3. What about . . .

...if you don't want to use the provided data controls? Is there a way to pass in a parameter to the query so that the appropriate order is displayed?

Yes there is. You can, of course, revert to the traditional ADO.NET object model, but there is no need to. Instead, you can add a parameter to the query maintained by DataSet.

Return to the Data Sources window and right-click NorthwindDataSet. Select Edit DataSet with Designer, as shown in Figure 5-13.

Figure 5-13. Editing the data set


This will open the Designer. Right-click the Orders table and choose Add Query, as shown in Figure 5-14.

Figure 5-14. Adding a query


This opens the TableAdapter Query Configuration Wizard. Click Next to begin. On the wizard page that opens, choose Use SQL Statements (or if you prefer, create a new stored procedure or use an existing one). The SQL statement we'll create will return one or more rows of data. In the next step in the wizard, modify the query by adding the highlighted text shown in Figure 5-15. Click Next.

Figure 5-15. Adding the where clause


In the next wizard step, name your methods for filling and getting the data tables to indicate the parameter you'll use, as shown in Figure 5-16.

Figure 5-16. Naming the methods


When you click Finish, OrdersTableAdapter has a new method, as shown in Figure 5-17.

Figure 5-17. New table added to OrdersTableAdapter


Alternatively, you can click the smart tag on OrdersTableAdapter (in the tray) and click Add Query, which brings up the Search Criteria Builder that lets you add a new query and name it all in one form, as shown in Figure 5-18.

Figure 5-18. The Search Criteria Builder


This second approach automatically adds a tool strip to your form to facilitate searching by the criteria you've set, as shown in Figure 5-19.

Figure 5-19. The Order Details tool strip


5.2.4. Where can I learn more?

MSDN has an excellent overview called "Accessing Data." Pay particular attention to the sections on using the .NET Framework.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net