NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

DataView Class

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

Object
   Component
      DataView

[Visual Basic]
Public Class DataView
   Inherits Component
   Implements ILiveList, IList, ICollection, IEnumerable, _
   ISortedList, IIndexedList, ITypedList
[C#]
public class DataView : Component, ILiveList, IList, ICollection,
   IEnumerable, ISortedList, IIndexedList, ITypedList
[C++]
public __gc class DataView : public Component, ILiveList, IList,
   ICollection, IEnumerable, ISortedList, IIndexedList, ITypedList
[JScript]
public class DataView extends Component, ILiveList, IList,
   ICollection, IEnumerable, ISortedList, IIndexedList, ITypedList

Remarks

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

Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data. For example, one control may be bound to a DataView showing all of the rows in the table, while a second may be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property which returns the default DataView for the table. For example, if you wish 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 (DataView indexer) 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.

Requirements

Namespace: System.Data

Assembly: System.Data.dll

Example [Visual Basic]

The following example creates a new DataView with and binds a control to it.

[Visual Basic]

Private Sub CreateDataView()
   Dim t As DataTable
   Set t = New DataTable   
   ' Not shown: code to populate the DataTable with data.
   Dim dv As DataView
   ' Create the DataView using the table.
   dv = New DataView(t)
   ' Set properties of the DataView assuming a table named Orders, 
   ' with fields for customer ID, date, ship date, and order total.
   With dv
      .RowFilter = "CustID = 'ANTON'"
      .Sort = "Date"
      .RowStateFilter = DataViewRowState.ModifiedCurrent
      .AllowDelete = True
      .AllowEdit = True
      .AllowNew = False
    End With

   ' Bind to a DataGrid control.
   DataGrid1.DataSource = dv
   DataGrid1.PopulateColumns
End Sub

Private Sub AddToDataView()
   ' Get the DataView from the DataGrid.
   Dim dv As DataView
   dv = CType(DataGrid1.DataSource, DataView)
   ' Add a new row.
   dv.AllowNew = True
   Dim drv As DataRowView
   drv = dv.AddNew()
   ' Set a value in one field of the row.
   drv("CompanyID") = 9877
End Sub

Private Sub DeleteRow()
   ' Get the DataView from the DataGrid.
   Dim dv As DataView
   dv = CType(DataGrid1.DataSource, DataView)
   dv.AllowDelete = True
   ' Delete the first row of the DataView
   dv.Delete(0)
   ' Print the count of deleted rows.
   dv.RowStateFilter = Deleted

See Also

DataView Members | System.Data Namespace | DataSet | DataTable | DataSetView