DekGenius.com
[ Team LiB ] Previous Section Next Section

10.5 The DataView Class

The master/details views shown earlier illustrate that the data-binding architecture is capable of showing a filtered view of the contents of a table—if you bind a DataGrid or a ListBox to a child of a relation in a data source, you will just see the related items in the child table instead of all of them. This relational view is just one of the ways in which we can filter the underlying data. The data-binding architecture also provides the DataView class, whose purpose is to provide modified views of tables.

We have already been using the DataView class implicitly. The DataSet class itself does not implement all the binding interfaces discussed earlier—it defers to the DataView. If you call the GetType method on the List property of a CurrencyManager for a DataSet, you will see that its type is not DataSet or DataTable; it is DataView.

The DataSet provides a default view for each of its tables, and this is the view to which controls will normally bind. We can modify the default view's properties, as Example 10-37 shows.

Example 10-37. Changing the sort order of the default view
// C# code
ds.Tables["Orders"].DefaultView.Sort = "ShippedDate";

' VB code
ds.Tables("Orders").DefaultView.Sort = "ShippedDate"

This modifies the default DataView for the Orders table, causing it to display the contents sorted by their ShippedDate column. The DataView class also provides a RowFilter property, allowing the view to filter out rows according to the specified criteria, as shown in Example 10-38.

Example 10-38. Filtering a DataView
// C# code
ds.Tables["Customers"].DefaultView.RowFilter = "Country = 'UK'";

' VB code
ds.Tables("Customers").DefaultView.RowFilter = "Country = 'UK'"

Row filter expressions provide a subset of the kind of functionality typically available with SQL WHERE clauses. They use the ADO.NET data expression language, a powerful language whose full capabilities are beyond the scope of this discussion. But to give a brief flavor of what is available, comparison operators are supported (for comparisons with constants, or with other column values), a LIKE operator is provided for wildcard filtering, and you can even navigate relations. For example, the row filter expression "Parent(MyRelation).Name LIKE 'Q*'" requires that the table being filtered is the child in a relation called MyRelation. A DataView with this filter will only show those rows for which the related row in the parent table's Name column begins with Q.

Controls usually use the default view for the data source to which they are bound, but you can specify a different view. Example 10-39 shows how to do this.

Example 10-39. Binding directly to a DataView
// C# code
DataView dv = new DataView(ds.Tables["Customers"]);
dv.RowFilter = "Count(Child(CustomerOrdersRelation).OrderID) > 20";

listBoxCustomers.DataSource = dv;
listBoxCustomers.DisplayMember = "CompanyName";

' VB code
Dim dv As New DataView(ds.Tables("Customers"))
dv.RowFilter = "Count(Child(CustomerOrdersRelation).OrderID) > 20"

listBoxCustomers.DataSource = dv
listBoxCustomers.DisplayMember = "CompanyName"

This creates a new DataView for the Customers table, and specifies this DataView as the DataSource for the ListBox control. It also illustrates the use of an aggregate function from the data expression language, Count. This filter will only show rows from the Customers table with more than 20 related entries in the Orders table.

    [ Team LiB ] Previous Section Next Section