5 out of 29 rated this helpful - Rate this topic

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

The DataView type exposes the following members.

  Name Description
Public method Supported by the XNA Framework DataView() Initializes a new instance of the DataView class.
Public method Supported by the XNA Framework DataView(DataTable) Initializes a new instance of the DataView class with the specified DataTable.
Public method Supported by the XNA Framework DataView(DataTable, String, String, DataViewRowState) Initializes a new instance of the DataView class with the specified DataTable, RowFilter, Sort, and DataViewRowState.
Top
  Name Description
Public property Supported by the XNA Framework AllowDelete Sets or gets a value that indicates whether deletes are allowed.
Public property Supported by the XNA Framework AllowEdit Gets or sets a value that indicates whether edits are allowed.
Public property Supported by the XNA Framework AllowNew Gets or sets a value that indicates whether the new rows can be added by using the AddNew method.
Public property Supported by the XNA Framework ApplyDefaultSort Gets or sets a value that indicates whether to use the default sort.
Public property Supported by the XNA Framework Container Gets the container for the component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework Count Gets the number of records in the DataView after RowFilter and RowStateFilter have been applied.
Public property Supported by the XNA Framework DataViewManager Gets the DataViewManager associated with this view.
Public property Supported by the XNA Framework DesignMode Gets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.)
Protected property Supported by the XNA Framework Events Gets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework IsInitialized Gets a value that indicates whether the component is initialized.
Protected property Supported by the XNA Framework IsOpen Gets a value that indicates whether the data source is currently open and projecting views of data on the DataTable.
Public property Supported by the XNA Framework Item Gets a row of data from a specified table.
Public property Supported by the XNA Framework RowFilter Gets or sets the expression used to filter which rows are viewed in the DataView.
Public property Supported by the XNA Framework RowStateFilter Gets or sets the row state filter used in the DataView.
Public property Supported by the XNA Framework Site Gets or sets the site of the component. (Inherited from MarshalByValueComponent.)
Public property Supported by the XNA Framework Sort Gets or sets the sort column or columns, and sort order for the DataView.
Public property Supported by the XNA Framework Table Gets or sets the source DataTable.
Top
  Name Description
Public method Supported by the XNA Framework AddNew Adds a new row to the DataView.
Public method Supported by the XNA Framework BeginInit Starts the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime.
Protected method Supported by the XNA Framework Close Closes the DataView.
Protected method Supported by the XNA Framework ColumnCollectionChanged Occurs after a DataColumnCollection has been changed successfully.
Public method Supported by the XNA Framework CopyTo Copies items into an array. Only for Web Forms Interfaces.
Public method Supported by the XNA Framework Delete Deletes a row at the specified index.
Public method Supported by the XNA Framework Dispose() Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.)
Protected method Supported by the XNA Framework Dispose(Boolean) Disposes of the resources (other than memory) used by the DataView object. (Overrides MarshalByValueComponent.Dispose(Boolean).)
Public method Supported by the XNA Framework EndInit Ends the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime.
Public method Supported by the XNA Framework Equals(DataView) Determines whether the specified DataView instances are considered equal.
Public method Supported by the XNA Framework Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework Find(Object) Finds a row in the DataView by the specified sort key value.
Public method Supported by the XNA Framework Find(Object[]) Finds a row in the DataView by the specified sort key values.
Public method Supported by the XNA Framework FindRows(Object) Returns an array of DataRowView objects whose columns match the specified sort key value.
Public method Supported by the XNA Framework FindRows(Object[]) Returns an array of DataRowView objects whose columns match the specified sort key value.
Public method Supported by the XNA Framework GetEnumerator Gets an enumerator for this DataView.
Public method Supported by the XNA Framework GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework GetService Gets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework IndexListChanged Occurs after a DataView has been changed successfully.
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework OnListChanged Raises the ListChanged event.
Protected method Supported by the XNA Framework Open Opens a DataView.
Protected method Supported by the XNA Framework Reset Reserved for internal use only.
Public method Supported by the XNA Framework ToString Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from MarshalByValueComponent.)
Public method Supported by the XNA Framework ToTable() Creates and returns a new DataTable based on rows in an existing DataView.
Public method Supported by the XNA Framework ToTable(String) Creates and returns a new DataTable based on rows in an existing DataView.
Public method Supported by the XNA Framework ToTable(Boolean, String[]) Creates and returns a new DataTable based on rows in an existing DataView.
Public method Supported by the XNA Framework ToTable(String, Boolean, String[]) Creates and returns a new DataTable based on rows in an existing DataView.
Protected method Supported by the XNA Framework UpdateIndex() Reserved for internal use only.
Protected method Supported by the XNA Framework UpdateIndex(Boolean) Reserved for internal use only.
Top
  Name Description
Public event Supported by the XNA Framework Disposed Adds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.)
Public event Supported by the XNA Framework Initialized Occurs when initialization of the DataView is completed.
Public event Supported by the XNA Framework ListChanged Occurs when the list managed by the DataView changes.
Top
  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework Cast<TResult> Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  Name Description
Explicit interface implemetation Private method Supported by the XNA Framework IBindingList.AddIndex For a description of this member, see IBindingList.AddIndex.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingList.AddNew For a description of this member, see IBindingList.AddNew.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.AllowEdit For a description of this member, see IBindingList.AllowEdit.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.AllowNew For a description of this member, see IBindingList.AllowNew.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.AllowRemove For a description of this member, see IBindingList.AllowRemove.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingList.ApplySort For a description of this member, see IBindingList.ApplySort.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingList.Find For a description of this member, see IBindingList.Find.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.IsSorted For a description of this member, see IBindingList.IsSorted.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingList.RemoveIndex For a description of this member, see IBindingList.RemoveIndex.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingList.RemoveSort For a description of this member, see IBindingList.RemoveSort.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.SortDirection For a description of this member, see IBindingList.SortDirection.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.SortProperty For a description of this member, see IBindingList.SortProperty.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.SupportsChangeNotification For a description of this member, see IBindingList.SupportsChangeNotification.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.SupportsSearching For a description of this member, see IBindingList.SupportsSearching.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingList.SupportsSorting For a description of this member, see IBindingList.SupportsSorting.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingListView.ApplySort For a description of this member, see IBindingListView.ApplySort.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingListView.Filter For a description of this member, see IBindingListView.Filter.
Explicit interface implemetation Private method Supported by the XNA Framework IBindingListView.RemoveFilter For a description of this member, see IBindingListView.RemoveFilter.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingListView.SortDescriptions For a description of this member, see IBindingListView.SortDescriptions.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingListView.SupportsAdvancedSorting For a description of this member, see IBindingListView.SupportsAdvancedSorting.
Explicit interface implemetation Private property Supported by the XNA Framework IBindingListView.SupportsFiltering For a description of this member, see IBindingListView.SupportsFiltering.
Explicit interface implemetation Private property Supported by the XNA Framework ICollection.IsSynchronized For a description of this member, see ICollection.IsSynchronized.
Explicit interface implemetation Private property Supported by the XNA Framework ICollection.SyncRoot For a description of this member, see ICollection.SyncRoot.
Explicit interface implemetation Private method Supported by the XNA Framework IList.Add For a description of this member, see IList.Add.
Explicit interface implemetation Private method Supported by the XNA Framework IList.Clear For a description of this member, see IList.Clear.
Explicit interface implemetation Private method Supported by the XNA Framework IList.Contains For a description of this member, see IList.Contains.
Explicit interface implemetation Private method Supported by the XNA Framework IList.IndexOf For a description of this member, see IList.IndexOf.
Explicit interface implemetation Private method Supported by the XNA Framework IList.Insert For a description of this member, see IList.Insert.
Explicit interface implemetation Private property Supported by the XNA Framework IList.IsFixedSize For a description of this member, see IList.IsFixedSize.
Explicit interface implemetation Private property Supported by the XNA Framework IList.IsReadOnly For a description of this member, see IList.IsReadOnly.
Explicit interface implemetation Private property Supported by the XNA Framework IList.Item For a description of this member, see IList.Item.
Explicit interface implemetation Private method Supported by the XNA Framework IList.Remove For a description of this member, see IList.Remove.
Explicit interface implemetation Private method Supported by the XNA Framework IList.RemoveAt For a description of this member, see IList.RemoveAt.
Explicit interface implemetation Private method Supported by the XNA Framework ITypedList.GetItemProperties For a description of this member, see ITypedList.GetItemProperties.
Explicit interface implemetation Private method Supported by the XNA Framework ITypedList.GetListName For 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.

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


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
ModifiedCurrent also includes Added

Hi there,
I tested DemonstrateDataView() to get a feel for what you are describing in your article and observerd that if I removed DataViewRowState.Added from:

secondView.RowStateFilter = DataViewRowState.ModifiedCurrent _
or DataViewRowState.Added

I would get the same output.  This was very confusing to me because I thought the entire purpose to include "... or DataViewRowState.Added " was to show the newly added item "fish"; and this would not be possible unless one uses the DataViewRowState.Added.  So, how come it is showing up?

However, after showing the code to my colleage Justin http://jwcooney.com , he concluded that the reason the added row was being captured by setting the secondview.RowStateFilter to SetDataViewRowState.ModifiedCurrent is because the was added but then also modified.

        CODE FROM THE EXAMPLE

        ' Add one New row to the second view.
        Dim rowView As DataRowView
        rowView = secondView.AddNew() '<--This step adds the row.  It's a done deal here.  The row is now part of the view
        rowView("item") = "fish"                 '<--The row is now being MODIFIED


To illustrate the use of  Data View States, this example would benefit from changing its code to:

        SUGGESTED EXAMPLE CODE CHANGE

 

        Dim rowView As DataRowView = secondView.Item(0)
        rowView("item") = "fish"
        rowView = secondView.AddNew()  '<--At this point the row has ONLY been ADDED
      

Otherwise, the fact that the output of this example yields the same answer with DataViewRowState.Added or without DataViewRowState.Added may lead one to believe that DataViewRowState.ModifiedCurrent also includes Added rows. 

I always learn lots from MSDN and found your example very helpful and hope you find my suggestion useful.

 

 

Happy Programming,

Luvy