ADO - ADO.NET DataViewManager for Managing Multiple DataViews

The DataViewManager is a class in ADO.NET that is used to manage multiple DataView objects associated with different DataTable objects within a DataSet. A DataView provides a customized view of the data in a table, allowing developers to apply sorting, filtering, and other display-related settings without modifying the original data. When an application works with several tables and needs separate views for each table, DataViewManager provides a centralized way to manage those views.

What is DataViewManager?

DataViewManager belongs to the System.Data namespace. It works primarily with a DataSet and maintains a collection of DataViewSetting objects. Each DataViewSetting represents the view configuration for a particular DataTable.

Instead of manually creating and configuring individual DataView objects, a developer can use DataViewManager to define how tables in a DataSet should be viewed. These settings can include properties such as:

  • RowFilter

  • Sort

  • ApplyDefaultSort

For example, suppose a DataSet contains Customers, Orders, and Products tables. An application may need to display customers alphabetically, orders above a particular amount, and products sorted by price. A DataViewManager can maintain these different view settings centrally.

Why is DataViewManager Useful?

A single DataView is useful when working with one particular table. However, applications often contain a DataSet with multiple related tables. Managing each table's view independently can make the code more difficult to organize.

DataViewManager provides a centralized view-management mechanism. It allows the application to configure the presentation of multiple tables while keeping the underlying DataTable data unchanged.

For example, an application could configure:

DataViewManager manager = new DataViewManager(dataSet);

manager.DataViewSettings["Customers"].Sort = "CustomerName";
manager.DataViewSettings["Orders"].RowFilter = "OrderAmount > 1000";

Here, the original Customers and Orders tables are not modified. The settings determine how their corresponding views should present the data.

Important Properties

The DataViewManager class provides several important members.

DataSet

The DataSet property identifies the DataSet whose tables are being managed.

DataViewManager manager = new DataViewManager(dataSet);

DataSet currentDataSet = manager.DataSet;

This allows the application to access the DataSet associated with the manager.

DataViewSettings

The DataViewSettings property is one of the most important features of DataViewManager. It provides a collection of DataViewSetting objects.

DataViewSetting setting =
    manager.DataViewSettings["Customers"];

A particular table can be selected from this collection and its view configuration can then be changed.

DataViewSetting

DataViewSetting represents the view configuration for an individual DataTable. It allows developers to specify how the rows of that table should be displayed.

Common properties include:

RowFilter

Specifies which rows should be included in the view.

manager.DataViewSettings["Orders"].RowFilter =
    "OrderAmount > 5000";

Only orders satisfying the specified condition will appear in the corresponding view.

Sort

Specifies the sorting expression.

manager.DataViewSettings["Customers"].Sort =
    "CustomerName ASC";

This causes the view to present customers in ascending order by name.

ApplyDefaultSort

Determines whether the default sorting behavior should be applied.

manager.DataViewSettings["Customers"].ApplyDefaultSort = true;

This can be useful when the underlying table has a primary key and the application wants the view to use the default sorting behavior.

Creating a DataViewManager

A DataViewManager can be created by passing a DataSet to its constructor.

DataSet dataSet = new DataSet();

DataTable customers = new DataTable("Customers");
customers.Columns.Add("CustomerID", typeof(int));
customers.Columns.Add("CustomerName", typeof(string));

customers.Rows.Add(1, "Rahul");
customers.Rows.Add(2, "Anita");
customers.Rows.Add(3, "Kiran");

dataSet.Tables.Add(customers);

DataViewManager manager = new DataViewManager(dataSet);

The manager is now associated with the DataSet and can be used to configure views for its tables.

Managing Multiple Tables

The main advantage of DataViewManager becomes clearer when a DataSet contains multiple tables.

For example:

manager.DataViewSettings["Customers"].Sort =
    "CustomerName ASC";

manager.DataViewSettings["Orders"].RowFilter =
    "OrderAmount > 1000";

manager.DataViewSettings["Products"].Sort =
    "Price DESC";

Each table can have its own independent view configuration.

The Customers view is sorted by customer name, the Orders view displays only orders above a certain amount, and the Products view is sorted according to price.

This makes DataViewManager useful for applications that need consistent management of several table views.

DataViewManager and DataView

Although both classes are related, they serve different purposes.

A DataView represents a customized view of a particular DataTable. It is commonly used when an application needs filtering or sorting for one table.

A DataViewManager, on the other hand, manages the view settings for multiple tables belonging to a DataSet.

For example:

DataSet
   |
   +-- Customers
   |      |
   |      +-- DataView settings
   |
   +-- Orders
   |      |
   |      +-- DataView settings
   |
   +-- Products
          |
          +-- DataView settings

Therefore, DataViewManager can be considered a centralized manager for the views of tables within a DataSet.

Example with Filtering and Sorting

Consider a DataSet containing an Employees table.

DataViewManager manager = new DataViewManager(dataSet);

DataViewSetting setting =
    manager.DataViewSettings["Employees"];

setting.RowFilter = "Department = 'Sales'";
setting.Sort = "EmployeeName ASC";

The resulting view displays only employees belonging to the Sales department and sorts them alphabetically.

The important point is that the filtering and sorting apply to the view, not directly to the underlying DataTable.

Advantages of DataViewManager

DataViewManager offers several benefits in applications that work extensively with DataSet objects.

Centralized management: View configurations for multiple tables can be maintained through one manager.

Preserves original data: Sorting and filtering do not change the actual rows stored in the DataTable.

Supports multiple tables: Different view settings can be applied to different tables within the same DataSet.

Improves organization: Applications with complex data presentation requirements can keep view-related settings separate from the actual data.

Useful with data-bound applications: Data views can be used by controls that support data binding, allowing different tables to be presented according to their configured settings.

Difference Between DataViewManager and DataSet

A DataSet is primarily a container for data. It can hold multiple DataTable objects, relationships, constraints, and other information.

DataViewManager does not replace the DataSet. Instead, it works with the DataSet to manage how its tables are viewed.

In simple terms:

DataSet
    |
    |-- Stores the data
    |
    +-- Customers
    +-- Orders
    +-- Products

DataViewManager
    |
    |-- Manages view settings
    |
    +-- Customers -> Sorting
    +-- Orders    -> Filtering
    +-- Products  -> Sorting/Filtering

When Should DataViewManager Be Used?

DataViewManager is particularly useful when an application has a DataSet containing several tables and needs different sorting or filtering rules for those tables.

For a single table, creating a DataView directly may be simpler. When several tables need independently configured views, DataViewManager provides a more organized approach.

It is especially relevant in traditional ADO.NET applications that use DataSet-based data access and data binding.

Summary

DataViewManager is an ADO.NET class designed to manage the view settings of multiple DataTable objects within a DataSet. Through its DataViewSettings collection, developers can configure filtering, sorting, and default sorting behavior for individual tables.

The key distinction is that DataView focuses on creating a customized view of a single table, whereas DataViewManager provides centralized management of view settings across multiple tables in a DataSet. It does not alter the underlying data; instead, it controls how that data is presented through its views.