Working with DataView

Working with DataView in C#

The DataView object provides a window into a data table that can be sorted and filtered. A data table can have many DataView objects assigned to it, so the data can be viewed in many ways without requiring it to be reread from the database. A major function of the DataView is to allow data binding on both Windows Forms and Web Forms.

We can perform several operations with DataView:

 

  • Sorting – The view of the data can be sorted based on one or more columns in ascending or descending order

          Ex :

        DataView dView = new DataView();

        dView.Sort = "CreatedDate DESC"

 

  • Filtering – The data visible through the view can be filtered with expressions based on one or more columns.
          Ex:
          dView.Table = DataSet1.Tables["Suppliers"];
          dView.RowFilter = "City = 'New Delhi'";

 

  • Row version filtering – The data visible through the view can be filtered based on the version of the rows.

          Ex:

          dView.RowStateFilter = DataViewRowState.ModifiedCurrent;

 

 

We can create a DataView using three ways:

  1. Retrieve the default view of a DataTable by using its DefaultView property.
  2. Create a new instance of a DataView that can than be associated with a DataTable.
  3. Use a DataViewManager to create a DataView for a DataTable.

 

Ebook Download
View all
Learn
View all