DataView Class

 

Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation. The DataView does not store data, but instead represents a connected view of its corresponding DataTable. Changes to the DataView’s data will affect the DataTable. Changes to the DataTable’s data will affect all DataViews associated with it.

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


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

NameDescription
System_CAPS_pubmethodDataView()

Initializes a new instance of the DataView class.

System_CAPS_pubmethodDataView(DataTable)

Initializes a new instance of the DataView class with the specified DataTable.

System_CAPS_pubmethodDataView(DataTable, String, String, DataViewRowState)

Initializes a new instance of the DataView class with the specified DataTable, RowFilter, Sort, and DataViewRowState.

NameDescription
System_CAPS_pubpropertyAllowDelete

Sets or gets a value that indicates whether deletes are allowed.

System_CAPS_pubpropertyAllowEdit

Gets or sets a value that indicates whether edits are allowed.

System_CAPS_pubpropertyAllowNew

Gets or sets a value that indicates whether the new rows can be added by using the AddNew method.

System_CAPS_pubpropertyApplyDefaultSort

Gets or sets a value that indicates whether to use the default sort. The default sort is (ascending) by all primary keys as specified by PrimaryKey.

System_CAPS_pubpropertyContainer

Gets the container for the component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubpropertyCount

Gets the number of records in the DataView after RowFilter and RowStateFilter have been applied.

System_CAPS_pubpropertyDataViewManager

Gets the DataViewManager associated with this view.

System_CAPS_pubpropertyDesignMode

Gets a value indicating whether the component is currently in design mode.(Inherited from MarshalByValueComponent.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubpropertyIsInitialized

Gets a value that indicates whether the component is initialized.

System_CAPS_protpropertyIsOpen

Gets a value that indicates whether the data source is currently open and projecting views of data on the DataTable.

System_CAPS_pubpropertyItem[Int32]

Gets a row of data from a specified table.

System_CAPS_pubpropertyRowFilter

Gets or sets the expression used to filter which rows are viewed in the DataView.

System_CAPS_pubpropertyRowStateFilter

Gets or sets the row state filter used in the DataView.

System_CAPS_pubpropertySite

Gets or sets the site of the component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubpropertySort

Gets or sets the sort column or columns, and sort order for the DataView.

System_CAPS_pubpropertyTable

Gets or sets the source DataTable.

NameDescription
System_CAPS_pubmethodAddNew()

Adds a new row to the DataView.

System_CAPS_pubmethodBeginInit()

Starts the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime.

System_CAPS_protmethodClose()

Closes the DataView.

System_CAPS_protmethodColumnCollectionChanged(Object, CollectionChangeEventArgs)

Occurs after a DataColumnCollection has been changed successfully.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies items into an array. Only for Web Forms Interfaces.

System_CAPS_pubmethodDelete(Int32)

Deletes a row at the specified index.

System_CAPS_pubmethodDispose()

Releases all resources used by the MarshalByValueComponent.(Inherited from MarshalByValueComponent.)

System_CAPS_protmethodDispose(Boolean)

Disposes of the resources (other than memory) used by the DataView object.(Overrides MarshalByValueComponent.Dispose(Boolean).)

System_CAPS_pubmethodEndInit()

Ends the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime.

System_CAPS_pubmethodEquals(DataView)

Determines whether the specified DataView instances are considered equal.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from MarshalByValueComponent.)

System_CAPS_pubmethodFind(Object)

Finds a row in the DataView by the specified sort key value.

System_CAPS_pubmethodFind(Object[])

Finds a row in the DataView by the specified sort key values.

System_CAPS_pubmethodFindRows(Object)

Returns an array of DataRowView objects whose columns match the specified sort key value.

System_CAPS_pubmethodFindRows(Object[])

Returns an array of DataRowView objects whose columns match the specified sort key value.

System_CAPS_pubmethodGetEnumerator()

Gets an enumerator for this DataView.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetService(Type)

Gets the implementer of the IServiceProvider.(Inherited from MarshalByValueComponent.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodIndexListChanged(Object, ListChangedEventArgs)

Occurs after a DataView has been changed successfully.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnListChanged(ListChangedEventArgs)

Raises the ListChanged event.

System_CAPS_protmethodOpen()

Opens a DataView.

System_CAPS_protmethodReset()

Reserved for internal use only.

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from MarshalByValueComponent.)

System_CAPS_pubmethodToTable()

Creates and returns a new DataTable based on rows in an existing DataView.

System_CAPS_pubmethodToTable(Boolean, String[])

Creates and returns a new DataTable based on rows in an existing DataView.

System_CAPS_pubmethodToTable(String)

Creates and returns a new DataTable based on rows in an existing DataView.

System_CAPS_pubmethodToTable(String, Boolean, String[])

Creates and returns a new DataTable based on rows in an existing DataView.

System_CAPS_protmethodUpdateIndex()

Reserved for internal use only.

System_CAPS_protmethodUpdateIndex(Boolean)

Reserved for internal use only.

NameDescription
System_CAPS_pubeventDisposed

Adds an event handler to listen to the Disposed event on the component.(Inherited from MarshalByValueComponent.)

System_CAPS_pubeventInitialized

Occurs when initialization of the DataView is completed.

System_CAPS_pubeventListChanged

Occurs when the list managed by the DataView changes.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Add(Object)

For a description of this member, see IList.Add.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Clear()

For a description of this member, see IList.Clear.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Contains(Object)

For a description of this member, see IList.Contains.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.IndexOf(Object)

For a description of this member, see IList.IndexOf.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Insert(Int32, Object)

For a description of this member, see IList.Insert.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Remove(Object)

For a description of this member, see IList.Remove.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.RemoveAt(Int32)

For a description of this member, see IList.RemoveAt.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingList.AddIndex(PropertyDescriptor)

For a description of this member, see IBindingList.AddIndex.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingList.AddNew()

For a description of this member, see IBindingList.AddNew.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingList.ApplySort(PropertyDescriptor, ListSortDirection)

For a description of this member, see IBindingList.ApplySort.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingList.Find(PropertyDescriptor, Object)

For a description of this member, see IBindingList.Find.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingList.RemoveIndex(PropertyDescriptor)

For a description of this member, see IBindingList.RemoveIndex.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingList.RemoveSort()

For a description of this member, see IBindingList.RemoveSort.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingListView.ApplySort(ListSortDescriptionCollection)

For a description of this member, see IBindingListView.ApplySort.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIBindingListView.RemoveFilter()

For a description of this member, see IBindingListView.RemoveFilter.

System_CAPS_pubinterfaceSystem_CAPS_privmethodITypedList.GetItemProperties(PropertyDescriptor[])

For a description of this member, see ITypedList.GetItemProperties.

System_CAPS_pubinterfaceSystem_CAPS_privmethodITypedList.GetListName(PropertyDescriptor[])

For a description of this member, see ITypedList.GetListName.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

For a description of this member, see ICollection.IsSynchronized.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

For a description of this member, see ICollection.SyncRoot.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsFixedSize

For a description of this member, see IList.IsFixedSize.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsReadOnly

For a description of this member, see IList.IsReadOnly.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.Item[Int32]

For a description of this member, see IList.Item.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.AllowEdit

For a description of this member, see IBindingList.AllowEdit.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.AllowNew

For a description of this member, see IBindingList.AllowNew.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.AllowRemove

For a description of this member, see IBindingList.AllowRemove.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.IsSorted

For a description of this member, see IBindingList.IsSorted.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.SortDirection

For a description of this member, see IBindingList.SortDirection.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.SortProperty

For a description of this member, see IBindingList.SortProperty.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.SupportsChangeNotification

For a description of this member, see IBindingList.SupportsChangeNotification.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.SupportsSearching

For a description of this member, see IBindingList.SupportsSearching.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingList.SupportsSorting

For a description of this member, see IBindingList.SupportsSorting.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingListView.Filter

For a description of this member, see IBindingListView.Filter.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingListView.SortDescriptions

For a description of this member, see IBindingListView.SortDescriptions.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingListView.SupportsAdvancedSorting

For a description of this member, see IBindingListView.SupportsAdvancedSorting.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIBindingListView.SupportsFiltering

For a description of this member, see IBindingListView.SupportsFiltering.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

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.

System_CAPS_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.RowsDataRowCollection.

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.

using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;

public class Form1: Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;



    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.AcceptChanges();
        table.Rows[0]["item"]="cat";
        table.Rows[1]["item"] = "dog";

        // 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;

.NET Framework
Available since 1.1

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

Return to top
Show: