Export (0) Print
Expand All

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

The DataView type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkDataView()Initializes a new instance of the DataView class.
Public methodSupported by the XNA FrameworkDataView(DataTable)Initializes a new instance of the DataView class with the specified DataTable.
Public methodSupported by the XNA FrameworkDataView(DataTable, String, String, DataViewRowState)Initializes a new instance of the DataView class with the specified DataTable, RowFilter, Sort, and DataViewRowState.
Top

  NameDescription
Public propertySupported by the XNA FrameworkAllowDeleteSets or gets a value that indicates whether deletes are allowed.
Public propertySupported by the XNA FrameworkAllowEditGets or sets a value that indicates whether edits are allowed.
Public propertySupported by the XNA FrameworkAllowNewGets or sets a value that indicates whether the new rows can be added by using the AddNew method.
Public propertySupported by the XNA FrameworkApplyDefaultSortGets 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.
Public propertySupported by the XNA FrameworkContainerGets the container for the component. (Inherited from MarshalByValueComponent.)
Public propertySupported by the XNA FrameworkCountGets the number of records in the DataView after RowFilter and RowStateFilter have been applied.
Public propertySupported by the XNA FrameworkDataViewManagerGets the DataViewManager associated with this view.
Public propertySupported by the XNA FrameworkDesignModeGets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.)
Protected propertySupported by the XNA FrameworkEventsGets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.)
Public propertySupported by the XNA FrameworkIsInitializedGets a value that indicates whether the component is initialized.
Protected propertySupported by the XNA FrameworkIsOpenGets a value that indicates whether the data source is currently open and projecting views of data on the DataTable.
Public propertySupported by the XNA FrameworkItemGets a row of data from a specified table.
Public propertySupported by the XNA FrameworkRowFilterGets or sets the expression used to filter which rows are viewed in the DataView.
Public propertySupported by the XNA FrameworkRowStateFilterGets or sets the row state filter used in the DataView.
Public propertySupported by the XNA FrameworkSiteGets or sets the site of the component. (Inherited from MarshalByValueComponent.)
Public propertySupported by the XNA FrameworkSortGets or sets the sort column or columns, and sort order for the DataView.
Public propertySupported by the XNA FrameworkTableGets or sets the source DataTable.
Top

  NameDescription
Public methodSupported by the XNA FrameworkAddNewAdds a new row to the DataView.
Public methodSupported by the XNA FrameworkBeginInitStarts the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime.
Protected methodSupported by the XNA FrameworkCloseCloses the DataView.
Protected methodSupported by the XNA FrameworkColumnCollectionChangedOccurs after a DataColumnCollection has been changed successfully.
Public methodSupported by the XNA FrameworkCopyToCopies items into an array. Only for Web Forms Interfaces.
Public methodSupported by the XNA FrameworkDeleteDeletes a row at the specified index.
Public methodSupported by the XNA FrameworkDispose()Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.)
Protected methodSupported by the XNA FrameworkDispose(Boolean)Disposes of the resources (other than memory) used by the DataView object. (Overrides MarshalByValueComponent.Dispose(Boolean).)
Public methodSupported by the XNA FrameworkEndInitEnds the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime.
Public methodSupported by the XNA FrameworkEquals(DataView)Determines whether the specified DataView instances are considered equal.
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.)
Public methodSupported by the XNA FrameworkFind(Object)Finds a row in the DataView by the specified sort key value.
Public methodSupported by the XNA FrameworkFind(Object[])Finds a row in the DataView by the specified sort key values.
Public methodSupported by the XNA FrameworkFindRows(Object)Returns an array of DataRowView objects whose columns match the specified sort key value.
Public methodSupported by the XNA FrameworkFindRows(Object[])Returns an array of DataRowView objects whose columns match the specified sort key value.
Public methodSupported by the XNA FrameworkGetEnumeratorGets an enumerator for this DataView.
Public methodSupported by the XNA FrameworkGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetServiceGets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkIndexListChangedOccurs after a DataView has been changed successfully.
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkOnListChangedRaises the ListChanged event.
Protected methodSupported by the XNA FrameworkOpenOpens a DataView.
Protected methodSupported by the XNA FrameworkResetReserved for internal use only.
Public methodSupported by the XNA FrameworkToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from MarshalByValueComponent.)
Public methodSupported by the XNA FrameworkToTable()Creates and returns a new DataTable based on rows in an existing DataView.
Public methodSupported by the XNA FrameworkToTable(String)Creates and returns a new DataTable based on rows in an existing DataView.
Public methodSupported by the XNA FrameworkToTable(Boolean, String[])Creates and returns a new DataTable based on rows in an existing DataView.
Public methodSupported by the XNA FrameworkToTable(String, Boolean, String[])Creates and returns a new DataTable based on rows in an existing DataView.
Protected methodSupported by the XNA FrameworkUpdateIndex()Reserved for internal use only.
Protected methodSupported by the XNA FrameworkUpdateIndex(Boolean)Reserved for internal use only.
Top

  NameDescription
Public eventSupported by the XNA FrameworkDisposedAdds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.)
Public eventSupported by the XNA FrameworkInitializedOccurs when initialization of the DataView is completed.
Public eventSupported by the XNA FrameworkListChangedOccurs when the list managed by the DataView changes.
Top

  NameDescription
Public Extension MethodAsParallel()Overloaded. Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsParallel()Overloaded. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryable()Overloaded. Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodAsQueryable()Overloaded. (Defined by Queryable.)
Public Extension MethodSupported by the XNA FrameworkCast<TResult>()Overloaded. Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkCast<TResult>()Overloaded. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkOfType<TResult>()Overloaded. Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkOfType<TResult>()Overloaded. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingList.AddIndexFor a description of this member, see IBindingList.AddIndex.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingList.AddNewFor a description of this member, see IBindingList.AddNew.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.AllowEditFor a description of this member, see IBindingList.AllowEdit.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.AllowNewFor a description of this member, see IBindingList.AllowNew.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.AllowRemoveFor a description of this member, see IBindingList.AllowRemove.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingList.ApplySortFor a description of this member, see IBindingList.ApplySort.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingList.FindFor a description of this member, see IBindingList.Find.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.IsSortedFor a description of this member, see IBindingList.IsSorted.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingList.RemoveIndexFor a description of this member, see IBindingList.RemoveIndex.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingList.RemoveSortFor a description of this member, see IBindingList.RemoveSort.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.SortDirectionFor a description of this member, see IBindingList.SortDirection.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.SortPropertyFor a description of this member, see IBindingList.SortProperty.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.SupportsChangeNotificationFor a description of this member, see IBindingList.SupportsChangeNotification.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.SupportsSearchingFor a description of this member, see IBindingList.SupportsSearching.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingList.SupportsSortingFor a description of this member, see IBindingList.SupportsSorting.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingListView.ApplySortFor a description of this member, see IBindingListView.ApplySort.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingListView.FilterFor a description of this member, see IBindingListView.Filter.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIBindingListView.RemoveFilterFor a description of this member, see IBindingListView.RemoveFilter.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingListView.SortDescriptionsFor a description of this member, see IBindingListView.SortDescriptions.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingListView.SupportsAdvancedSortingFor a description of this member, see IBindingListView.SupportsAdvancedSorting.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIBindingListView.SupportsFilteringFor a description of this member, see IBindingListView.SupportsFiltering.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkICollection.IsSynchronizedFor a description of this member, see ICollection.IsSynchronized.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkICollection.SyncRootFor a description of this member, see ICollection.SyncRoot.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.AddFor a description of this member, see IList.Add.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.ClearFor a description of this member, see IList.Clear.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.ContainsFor a description of this member, see IList.Contains.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.IndexOfFor a description of this member, see IList.IndexOf.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.InsertFor a description of this member, see IList.Insert.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIList.IsFixedSizeFor a description of this member, see IList.IsFixedSize.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIList.IsReadOnlyFor a description of this member, see IList.IsReadOnly.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIList.ItemFor a description of this member, see IList.Item.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.RemoveFor a description of this member, see IList.Remove.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIList.RemoveAtFor a description of this member, see IList.RemoveAt.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkITypedList.GetItemPropertiesFor a description of this member, see ITypedList.GetItemProperties.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkITypedList.GetListNameFor a description of this member, see ITypedList.GetListName.
Top

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.

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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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

Show:
© 2014 Microsoft