After discussing DataAdapters and data connections, you should have a pretty good idea of how to take advantage of the VS .NET design-time support to develop databound Windows Form database applications.
The DataSet and DataView components are two powerful and easy-to-use components of the ADO.NET model. In this section, you'll learn how to utilize DataSet and DataView components at design-time. In Chapter 4, we discuss their properties and methods in more detail and show how to use them programmatically. The DataSet and DataView components fall into the disconnected components category, which means you can use these components with or without data providers. We discuss disconnected data components in Chapter 3.
There are two types of DataSets: typed and untyped. As discussed in Chapter 1 (and in more detail in Chapter 4), a typed DataSet has an XML schema attached to it. The XML schema defines members for a DataSet corresponding to database table columns, and you can access data through these columns. Untyped datasets are ones that are created at runtime and don't have a schema attached to them. In the following section, we show you how you can generate typed datasets using a VS .NET wizard.
As you saw earlier, you can generate typed datasets by using the Generate Dataset option from a DataAdapter's Properties dialog box. And as mentioned earlier, the Generate DataSet option adds an XML schema to the project and also adds a class to the project inherited from the DataSet class. The designer looks like Figure 2-4.
Figure 2-4: A DataSet's Properties window showing a typed DataSet
Again, you can see a typed DataSet's properties by right-clicking the DataSet and using the Properties menu option.
Every DataSet generated by the IDE creates an XML schema for the DataSet. If you look at the bottom of the Properties window of a DataSet, you'll see two links: View Schema and DataSet Properties. The View Schema option lets you view the DataSet schema, and the DataSet Properties hyperlink lets you set the DataSet properties. By following these links you can also set the DataSet's column names and other properties.
Now you can use this DataSet as discussed earlier.
In the previous discussion, you saw how you can generate DataSet objects from a DataAdapter. There are also other ways to create a typed DataSet object.
You can click the Project menu and choose Add New Item. This brings up the Add New Item window where you'll find the DataSet template (see Figure 2-5). The default name of the DataSet template is DataSet1.xsd, but you can change it by entering something in the Name text box.
Figure 2-5: Creating a typed DataSet from the Add New Item window
After adding the DataSet, the designer creates an XSD (XML Schema) file and adds it your project area. By default the schema is empty the first time you add a DataSet schema using the Add New Item. You can see the contents of a DataSet schema by double-clicking DataSet1.xsd in the Solution Explorer. As you can see from Figure 2-6, the designer has two options: the Server Explorer and the Toolbox. If you click the Server Explorer link, it launches the Server Explorer. The XML designer has its own Toolbox, which contains various XML schema items.
Figure 2-6: Empty Dataset1.xsd in the VS .NET IDE
The next step is to fill this XML schema with data and connect to a database. The easiest way to do this is to use the Server Explorer. Open the Server Explorer by clicking the Sever Explorer link in the designer. You can expand any database and simply drop a database object such as a table, a view, or a stored procedure on the designer from the Server Explorer. You can even drop multiple database objects. To keep it simple, drop only one table, the Employees table from the SQL Server Northwind database.
Dragging the Employees table to the designer adds one XML schema (see Figure 2-7).
Figure 2-7: Design view of the DataSet's XML schema
It also automatically adds the typed DataSet class that inherits from DataSet. If you go to the Class View, you can see the DataSet1 class. If you expand this class, you'll notice this class is derived from the DataSet class. You can explore the code added by the designer. However, you'll never use these class members directly.
Once you have a typed DataSet, you can bind it to any data-bound control (as discussed earlier in this chapter).
A DataView represents a view of a DataSet object.
Why do I need a DataView when I can use a DataSet's DefaultViewManager?
Actually, the DefaultViewManager provides a view of all the available tables in a DataSet. What if you want to display only one table? Or what if you want to filter or sort data?
A DataView provides you with Filter and Sort properties; by using them you can filter and sort the data. There are many occasions when you don't want all data from a table. You can apply a filter criteria on a DataView and bind the DataView to the data-bound controls. You can also separate multiple tables of a DataSet by associating each table with a DataViewl. For example, you can create a DataSet with three tables and create three different DataView objects for each table. Once you have a DataView object, you can attach it with any data-bound control, such as a DataGrid or a ComboBox control using the data-bound control's DataSource property. Or you can filter only records based on a date or an ID.
In this chapter, we concentrate on the VS .NET part of DataView. We discuss the DataView class and its members in more detail in Chapter 4.
Creating and attaching a DataView with a data source is simple in the designer. In this sample, you'll create and attach a DataView with a DataSet. Before creating a DataView, though, you need to have a DataSet.
First, you create a DataAdapter by simply dragging a database table from the Server Explorer to the form. This action adds a Connection and a DataAdapter to the application and makes them available at design-time. Second, you generate a DataSet from a DataAdapter by selecting the Generate Dataset menu option available after right-clicking the DataAdapter.
Now you need to add a DataView. You simply drag a DataView by dragging the DataView control from Toolbox Data onto your form. This action adds a DataView1 to the project.
Next, attach DataView1 to the DataSet. You can do that by simply setting the Table property of the DataView. The Table property of DataView1 lists the DataSet and DataSet tables. In this case, select DataSet11.Employees as the Table property of DataView1 (see Figure 2-8).
Figure 2-8: DataView Properties window
This is where the DataView is useful. The DataView has two properties: Sort and RowFilter. The Sort property takes an expression with the name of the column followed by the sort order. For example, if you want to sort data based on the FirstName column in ascending order, you set the FirstName ASC string as the Sort property of DataView.
The RowFilter property filters the data of a DataView. This property works as a WHERE clause of the SELECT statement. The RowFilter property takes an expression with the column name and the criteria. For example, if you want to retrieve records where the country is USA, you simply set "Country='USA'" as the filter. As you can see in Figure 2-8, we set the RowFilter and Sort properties.
To sort in descending order, use DESC instead of ASC.
Once you have a DataView, you can bind it to a data-bound control such as a DataGrid by setting data-bound control's DataSource property. To test this, you can drop a DataGrid control onto the form and set DataGrid's DataSource property as DataView1. You can simply set that from the Properties window of DataGrid.
Now if you compile and run your project, you'll only see USA records sorted on the FirstName column (see Figure 2-9).
Figure 2-9: Sorted and filtered data in a DataGrid