Using DataSet, DataView, and DataViewManager


This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".
 

ADO.NET provides components to view data in data bound controls. These components are easy to use and provide rapid development. You just drag and drop these controls to a form, set their properties, and bind them to Data- bound controls. Then you're all set to run your programs. The DataView and DataViewManager classes fall in this category. You can use these classes to represent different views of a data table based on different filter and sort criterion. You can use these classes either at design-time or run-time modes. Earlier, you've seen how use these class either at design-time modes. You've also seen how to use class objects at design-time. In this article, you'll see how to use these classes programmatically.

A DataSet is a key component in the ADO.NET data model. It's an in-memory representation of one or multiple data tables, relations, and constraints. It provides a communication between the actual data Windows forms and Web Forms controls to provide the data a view through the DataView and the DataViewManager. Not only this, but a DataSet offers much more than that.

The DataView is useful when you need to bind a DataTable or multiple DataTable objects with data-bound controls. You can also represent multiple views of same data by applying a filter and sort on a data table, and you can bind these multiple views to different data-bound controls such as a DataGrid, DataList, comboBox, and ListBox control.

Figure 1 shows the relationship between a DataSet, DataTable, DataView, and Windows and Web Forms controls. As you can see from figure 1, a Data Set contains and Web Forms controls. As you can see from figure 1, a DataSet contains three DataTable objects, which are represented by three different views. The three different DataView objects bind to different data bound controls.

f1.png

Figure 1: Relation ship between the DataSet, DataTable, and DataView

The DataSet

A DataSet object plays a vital role in the ADO.NET component model. A DataSet represents disconnected cache of Data in the form of tables, rows, columns, or XML schemas. If you've ever programmed a database application in previous versions of Visual studio, you're probably familiar with recordset. A recordset object was a way to represent data in your programs. Similar to a recordset, a DataSet represent data in your applications. Once you have constructed a DataSet, you can get or set data in your Applications or your data source. As mentioned earlier, the DataSet works in both connected and disconnected environment. A DataSet communicates with a DataAdapter and calls DataAdapter's Fill method to fill data from a DataAdapters. You can fill a DataSet object with multiple tables or stored from a DataAdapter. In this article, you'll see how it works with DataTable and DataView objects.

As you can see in figure 2, the Fill method of data adapter fills data from a data adapter to a dataset. Once data is filled to a DataSet from a DataAdapter, you can view it in Windows or Web applications by binding data to data-bound through a DataView. You can generate one or multiple views for a data tables based on the filter and sort criteria.

datasetDataDapterDataView.gif

Figure 2: The relationship between DataSet, DataAdapter, and DataView objects

The DataSet class represents a dataset in ADO.NET. As you've seen earlier, a DataSet can have multiple DataTable objects. The DataTableCollection object represents all DataSet objects related to DataSet. The Tables property of the Dataset represents the collection of DataTable objects. The tables relate to each other with DataRelation objects, discussed earlier in this article. The DataRelationCollection represents all available relations in the DataSet. The Relations property represents the DataRelationCollection of DataSet. Table 1 describes some DataSet properties.

A DataSet object stores data in XML format. An XML schema represents a dataset. A DataSet also defines methods to read and write XML documents. The ReadXml method reads an XML document and fills the DataSet with the XML data and the WriteXML method writes DataSet data to an XML document. This class also defines the methods ReadXMLSchema and WriteXMLSchema to read and write XML schema. Table 2 describes some of the DataSet class members. I'll use these methods and properties through out this article's examples.

Table 1: The DataSet Class Properties

PROPERTY

DESCRIPTION

DataSetName

Represent the name of the Dataset

DefaultViewManager

Default view of the data of a DataSet

Relations

Collection of relations of a DataSet that links multiple tables

Tables

Collection of tables contained in a DataSet


Table 2: The Data Set Class Methods

METHOD

DESCRIPTION

AcceptChanges

Commits all the changes made since last AcceptChanges called

BeginInit

Begins initialization if a DataSet was used previously

Clear

Removes all data from a DataSet

Clone

Clones the structure of a DataSet

Copy Copies a dataset's data and structure

EndInit

Ends the initialization

GetChanges

Gets a copy of dataset containing all changes made since last AcceptChanges was called or loaded

GetXml

Returns the XML representation of the data stored in a DataSet

GetXmlSchema

Returns XML schema of the data stored in a DataSet

Merge

Merges two DataSet objects

ReadXmlSchema

Reads an XML schema and fills data in a DataSet

RejectChanges

Rejects all changes made to the DataSet since it was created or AcceptChanges called

Reset

Reset a DataSet to its original state

WriteXml

Write data of a DataSet to an XML document

WriteXmlSchema

Write data of a DataSet to an XML schema


Typed and Untyped DataSets

There are two kinds of DataSet: typed or untyped. A typed DataSet first comes from the DataSet class and then uses XML schema (.xsd file) to generate a new class. You've seen how to create a typed dataset using VS.NET earlier. An untyped dataset has no build-in schema. You create an instance of DataSet class and call its methods and properties to work with the Data Source. All elements of an untyped dataset are collections. In this article, you'll work with untyped datasets.

Both kinds of dataset have their own advantages and disadvantages. Typed datasets take less time to write applications but offer no flexibility. They're useful when you already know the schema of a database. The biggest advantage of typed datasets is the VS.NET IDE support you can drag a database table, its columns or stored procedures to a form in your application and the IDE generates typed dataset for you. After that you can bind these datasets to the controls. There are many occasions when you don't know the schema of a database. In those cases, the untyped datasets are useful. The untyped datasets also provide the flexibility of connecting with multiple data source. And you can use them without the VS. NET IDE.

The DataView

Another powerful feature of ADO.NET is the ability to create several different views of the same data. You can sort these views differently and filter them on different criteria. They can contain different row state information.

As you've seen in figure 5-18, a DataView represents a customized view of data table and can bind to windows Forms and Web Forms controls. Using DataView sort and filter features, you can also have multiple views of a single data table. Using RowFilter and Sort properties, you can apply a filter on a DataView and sort its contents before binding it to a data-bound control. The AddNew method adds a new row to a Dataview, and the Delete method deletes a row from a DataView. You can use the Find and FindRows methods to search for rows based on the defined criteria.

Table 3 describes some of the DataView properties, and Table 4 describes some of its methods.

Table 3: The DataView Class properties


PROPERTIES

DESCRIPTION

AllowDelete

Indicates weather deletes are allowed

AllowEdit

Indicates whether edits are allowed

AddNews

Indicates whether new rows can be added

Count

Represents the number of records in a DataView after RowFilter and RowstateFilter have been applied

DataViewManager

DataViewManager associates with this view

Item

Represent an item of a row

RowFilter

Represent the expression used to filter rows to view in the DataView

Sort

Represent the sort column and sort order

Table

DataTable attached with this view


Table 4: The DataView Class Methods

METHOD

DESCRIPTION

AddNew

Add a new row to the DataView

BeginInit

Begins the initialization if a data view was previously used

Delete

Deletes a row

Find

Finds a row in the DataView based on the specified criteria

FindRows

Returns an array of rows based on the specified criteria


The DataView Manager

A DataViewManager contains a collection of views of a dataset, one view for each data table in the dataset. The DataViewManager has a DataViewSettings property that enables the user to construct a different view for each data table in the dataset. If you want to create two views on the same data table, you need to create another instance of the DataViewManager and construct the DataViewSetting for that particular view. Then you construct a DataView using the DataViewManager. For example, in the Orders DataSet, you may want to filter out the orders with an EmployeedId of 4 and sort the orders by the date they were shipped. You can retrieve records using the sort and filter properties of the DataSet and attach the filtered and sorted data to DataView or a DataViewManager.

To construct a Data View Manager you can either use the Default constructor or pass in a DataSet object. For example:

view = new DataViewManager();
Or
view = new DataSetView(myDataSet);

The DataViewManager has a few properties you need to know about to utilize it effectively. Table 5 shows the main properties.

Table 5: Data Set View properties


PROPERTY

DESCRIPTION

DataSet

The DataSet being viewed of type DataSet

DataViewSettings

Contains the collection of TableSetting object for each table in the DataSet. The Table Setting object contains sorting and filtering criteria for a particular table.


The DataViewManager contains the CreateDataView method allows you to create a DataView object for a particular table in your DataSet. You can construct the DataView for the table with the DataViewManager's DataViewSetting for the particular DataTable of the DataSet. You can also adjust settings for the DataView by assigning filter and sort properties directly in the DataView.

Conclusion

Hope this article would have helped you in understanding the DataSet, DataView and DataViewManager, Typed and Untyped DataSets in ADO.NET. See my other articles on the website on ADO.NET.

Up Next
    Ebook Download
    View all
    Learn
    View all