Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
User Action:

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
	Inherits MarshalByValueComponent
	Implements IBindingListView, IBindingList, IList, ICollection,
	IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize
Name Description
System_CAPS_pubmethod DataView()

Initializes a new instance of the DataView class.

System_CAPS_pubmethod DataView(DataTable)

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

System_CAPS_pubmethod DataView(DataTable, String, String, DataViewRowState)

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

Name Description
System_CAPS_pubproperty AllowDelete

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

System_CAPS_pubproperty AllowEdit

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

System_CAPS_pubproperty AllowNew

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

System_CAPS_pubproperty ApplyDefaultSort

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_pubproperty Container

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

System_CAPS_pubproperty Count

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

System_CAPS_pubproperty DataViewManager

Gets the DataViewManager associated with this view.

System_CAPS_pubproperty DesignMode

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

System_CAPS_protproperty Events

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

System_CAPS_pubproperty IsInitialized

Gets a value that indicates whether the component is initialized.

System_CAPS_protproperty IsOpen

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

System_CAPS_pubproperty Item[Int32]

Gets a row of data from a specified table.

System_CAPS_pubproperty RowFilter

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

System_CAPS_pubproperty RowStateFilter

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

System_CAPS_pubproperty Site

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

System_CAPS_pubproperty Sort

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

System_CAPS_pubproperty Table

Gets or sets the source DataTable.

Name Description
System_CAPS_pubmethod AddNew()

Adds a new row to the DataView.

System_CAPS_pubmethod BeginInit()

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

System_CAPS_protmethod Close()

Closes the DataView.

System_CAPS_protmethod ColumnCollectionChanged(Object, CollectionChangeEventArgs)

Occurs after a DataColumnCollection has been changed successfully.

System_CAPS_pubmethod CopyTo(Array, Int32)

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

System_CAPS_pubmethod Delete(Int32)

Deletes a row at the specified index.

System_CAPS_pubmethod Dispose()

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

System_CAPS_protmethod Dispose(Boolean)

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

System_CAPS_pubmethod EndInit()

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

System_CAPS_pubmethod Equals(DataView)

Determines whether the specified DataView instances are considered equal.

System_CAPS_pubmethod Equals(Object)

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

System_CAPS_protmethod Finalize()

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_pubmethod Find(Object)

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

System_CAPS_pubmethod Find(Object[])

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

System_CAPS_pubmethod FindRows(Object)

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

System_CAPS_pubmethod FindRows(Object[])

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

System_CAPS_pubmethod GetEnumerator()

Gets an enumerator for this DataView.

System_CAPS_pubmethod GetHashCode()

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

System_CAPS_pubmethod GetService(Type)

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

System_CAPS_pubmethod GetType()

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

System_CAPS_protmethod IndexListChanged(Object, ListChangedEventArgs)

Occurs after a DataView has been changed successfully.

System_CAPS_protmethod MemberwiseClone()

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

System_CAPS_protmethod OnListChanged(ListChangedEventArgs)

Raises the ListChanged event.

System_CAPS_protmethod Open()

Opens a DataView.

System_CAPS_protmethod Reset()

Reserved for internal use only.

System_CAPS_pubmethod ToString()

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

System_CAPS_pubmethod ToTable()

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

System_CAPS_pubmethod ToTable(Boolean, String[])

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

System_CAPS_pubmethod ToTable(String)

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

System_CAPS_pubmethod ToTable(String, Boolean, String[])

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

System_CAPS_protmethod UpdateIndex()

Reserved for internal use only.

System_CAPS_protmethod UpdateIndex(Boolean)

Reserved for internal use only.

Name Description
System_CAPS_pubevent Disposed

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

System_CAPS_pubevent Initialized

Occurs when initialization of the DataView is completed.

System_CAPS_pubevent ListChanged

Occurs when the list managed by the DataView changes.

Name Description
System_CAPS_pubinterface System_CAPS_privmethod IList.Add(Object)

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

System_CAPS_pubinterface System_CAPS_privmethod IList.Clear()

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

System_CAPS_pubinterface System_CAPS_privmethod IList.Contains(Object)

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

System_CAPS_pubinterface System_CAPS_privmethod IList.IndexOf(Object)

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

System_CAPS_pubinterface System_CAPS_privmethod IList.Insert(Int32, Object)

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

System_CAPS_pubinterface System_CAPS_privmethod IList.Remove(Object)

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

System_CAPS_pubinterface System_CAPS_privmethod IList.RemoveAt(Int32)

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingList.AddIndex(PropertyDescriptor)

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingList.AddNew()

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingList.ApplySort(PropertyDescriptor, ListSortDirection)

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingList.Find(PropertyDescriptor, Object)

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingList.RemoveIndex(PropertyDescriptor)

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingList.RemoveSort()

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingListView.ApplySort(ListSortDescriptionCollection)

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

System_CAPS_pubinterface System_CAPS_privmethod IBindingListView.RemoveFilter()

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

System_CAPS_pubinterface System_CAPS_privmethod ITypedList.GetItemProperties(PropertyDescriptor[])

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

System_CAPS_pubinterface System_CAPS_privmethod ITypedList.GetListName(PropertyDescriptor[])

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

System_CAPS_pubinterface System_CAPS_privproperty ICollection.IsSynchronized

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

System_CAPS_pubinterface System_CAPS_privproperty ICollection.SyncRoot

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

System_CAPS_pubinterface System_CAPS_privproperty IList.IsFixedSize

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

System_CAPS_pubinterface System_CAPS_privproperty IList.IsReadOnly

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

System_CAPS_pubinterface System_CAPS_privproperty IList.Item[Int32]

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.AllowEdit

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.AllowNew

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.AllowRemove

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.IsSorted

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.SortDirection

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.SortProperty

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.SupportsChangeNotification

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.SupportsSearching

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingList.SupportsSorting

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingListView.Filter

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingListView.SortDescriptions

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingListView.SupportsAdvancedSorting

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

System_CAPS_pubinterface System_CAPS_privproperty IBindingListView.SupportsFiltering

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

Name Description
System_CAPS_pubmethod AsParallel()

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

System_CAPS_pubmethod AsQueryable()

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

System_CAPS_pubmethod Cast<TResult>()

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

System_CAPS_pubmethod OfType<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_note Note

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.

Private Sub DemonstrateDataView()
    ' Create one DataTable with one column.
    Dim table As DataTable = New DataTable("table")
    Dim colItem As DataColumn = New DataColumn("item", _
        Type.GetType("System.String"))
    table.Columns.Add(colItem)

    ' Add five items.
    Dim NewRow As DataRow
    Dim i As Integer
    For i = 0 To 4

    NewRow = table.NewRow()
    NewRow("item") = "Item " & i
    table.Rows.Add(NewRow)
    Next
    table.AcceptChanges()

    ' Create two DataView objects with the same table.
    Dim firstView As DataView = New DataView(table)
    Dim secondView As DataView = New DataView(table)

    ' Change the values in the table.
    table.Rows(0)("item") = "cat"
    table.Rows(1)("item") = "dog"

    ' 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.
    Dim rowView As DataRowView
    rowView = secondView.AddNew()
    rowView("item") = "fish"
    ' Set second DataView to show modified versions of 
    ' current rows, or New rows.
    secondView.RowStateFilter = DataViewRowState.ModifiedCurrent _
        Or DataViewRowState.Added
    ' Print modified and Added rows.
    PrintTableOrView(secondView, _
        "Second DataView: ModifiedCurrent or Added")
End Sub

Overloads Private Sub PrintTableOrView( _
    ByVal view As DataView, ByVal label As String)
    Console.WriteLine(label)
    Dim i As Integer
    For i = 0 To view.count - 1

    Console.WriteLine(view(i)("item"))
    Next
    Console.WriteLine()
End Sub

Overloads Private Sub PrintTableOrView( _
    ByVal table As DataTable, ByVal label As String)
    Console.WriteLine(label)
    Dim i As Integer
    For i = 0 To table.Rows.Count - 1
    Console.WriteLine(table.Rows(i)("item"))
    Next
    Console.WriteLine()
End Sub

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

Dim orders As DataTable = dataSet.Tables("SalesOrderHeader")

Dim query = _
    From order In orders.AsEnumerable() _
    Where order.Field(Of Boolean)("OnlineOrderFlag") = True _
    Order By order.Field(Of Decimal)("TotalDue") _
    Select order

Dim view As DataView = 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