Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
6 out of 38 rated this helpful - Rate this topic

DataView Class

Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

public class DataView : MarshalByValueComponent, IBindingListView, IBindingList, IList, 
	ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
public class DataView extends MarshalByValueComponent implements IBindingListView, IBindingList, 
	IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, 
	ISupportInitialize
public class DataView extends MarshalByValueComponent implements IBindingListView, IBindingList, 
	IList, ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, 
	ISupportInitialize

A major function of the DataView is to allow for data binding on both Windows Forms and Web Forms.

Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability lets you have two controls bound to the same DataTable, but that show different versions of the data. For example, one control might be bound to a DataView that shows all the rows in the table, and a second might be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property. This returns the default DataView for the table. For example, if you want to create a custom view on the table, set the RowFilter on the DataView returned by the DefaultView.

To create a filtered and sorted view of data, set the RowFilter and Sort properties. Then, use the Item property to return a single DataRowView.

You can also add and delete from the set of rows using the AddNew and Delete methods. When you use those methods, the RowStateFilter property can set to specify that only deleted rows or new rows be displayed by the DataView.

The following example creates a single DataTable with one column and five rows. Two DataView objects are created and the RowStateFilter is set on each to show different views of the table data. The values are then printed.

private void DemonstrateDataView()
{
    // Create one DataTable with one column.
    DataTable table = new DataTable("table");
    DataColumn colItem = new DataColumn("item",
        Type.GetType("System.String"));
    table.Columns.Add(colItem);

    // Add five items.
    DataRow NewRow;
    for(int i = 0; i <5; i++)
    {
        NewRow = table.NewRow();
        NewRow["item"] = "Item " + i;
        table.Rows.Add(NewRow);
    
    // Change the values in the table.
    table.Rows[0]["item"]="cat";
    table.Rows[1]["item"] = "dog";
    table.AcceptChanges();
 
    // Create two DataView objects with the same table.
    DataView firstView = new DataView(table);
    DataView secondView = new DataView(table);
 
    // Print current table values.
    PrintTableOrView(table,"Current Values in Table");

    // Set first DataView to show only modified 
    // versions of original rows.
    firstView.RowStateFilter=DataViewRowState.ModifiedOriginal;

    // Print values.   
    PrintTableOrView(firstView,"First DataView: ModifiedOriginal");

    // Add one New row to the second view.
    DataRowView rowView;
    rowView=secondView.AddNew();
    rowView["item"] = "fish";

    // Set second DataView to show modified versions of 
    // current rows, or New rows.
    secondView.RowStateFilter=DataViewRowState.ModifiedCurrent 
        | DataViewRowState.Added;
    // Print modified and Added rows.
    PrintTableOrView(secondView, 
        "Second DataView: ModifiedCurrent | Added");

 
private void PrintTableOrView(DataTable table, string label)
{
    // This function prints values in the table or DataView.
    Console.WriteLine("\n" + label);
    for(int i = 0; i<table.Rows.Count;i++)
    {
        Console.WriteLine("\table" + table.Rows[i]["item"]);
    
    Console.WriteLine();

 
private void PrintTableOrView(DataView view, string label)
{
 
    // This overload prints values in the table or DataView.
    Console.WriteLine("\n" + label);
    for(int i = 0; i<view.Count;i++)
    {
        Console.WriteLine("\table" + view[i]["item"]);
    
    Console.WriteLine();


This type is safe for multithreaded read operations. You must synchronize any write operations.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.