Export (0) Print
Expand All

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

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.

NoteNote:

If you do not explicitly specify sort criteria for DataView, the DataRowView objects in DataView are sorted based on the index of DataView's corresponding DataRow in the DataTable.Rows DataRowCollection.

LINQ to DataSet allows developers to create complex, powerful queries over a DataSet by using LINQ. A LINQ to DataSet query returns an enumeration of DataRow objects, however, which is not easily used in a binding scenario. DataView can be created from a LINQ to DataSet query and takes on the filtering and sorting characteristics of that query. LINQ to DataSet extends the functionality of the DataView by providing LINQ expression-based filtering and sorting, which allows for much more complex and powerful filtering and sorting operations than string-based filtering and sorting. See Data Binding and LINQ to DataSet for more information.

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();
}

The following example creates a DataView of online orders ordered by total due from a LINQ to DataSet query:

DataTable orders = dataSet.Tables["SalesOrderHeader"];

EnumerableRowCollection<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<bool>("OnlineOrderFlag") == true 
    orderby order.Field<decimal>("TotalDue")
    select order;

DataView view = query.AsDataView();

bindingSource1.DataSource = view;

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft