DataView Class
Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation.
Assembly: System.Data (in System.Data.dll)
The DataView type exposes the following members.
| Name | Description | |
|---|---|---|
|
DataView() | Initializes a new instance of the DataView class. |
|
DataView(DataTable) | Initializes a new instance of the DataView class with the specified DataTable. |
|
DataView(DataTable, String, String, DataViewRowState) | Initializes a new instance of the DataView class with the specified DataTable, RowFilter, Sort, and DataViewRowState. |
| Name | Description | |
|---|---|---|
|
AllowDelete | Sets or gets a value that indicates whether deletes are allowed. |
|
AllowEdit | Gets or sets a value that indicates whether edits are allowed. |
|
AllowNew | Gets or sets a value that indicates whether the new rows can be added by using the AddNew method. |
|
ApplyDefaultSort | Gets or sets a value that indicates whether to use the default sort. |
|
Container | Gets the container for the component. (Inherited from MarshalByValueComponent.) |
|
Count | Gets the number of records in the DataView after RowFilter and RowStateFilter have been applied. |
|
DataViewManager | Gets the DataViewManager associated with this view. |
|
DesignMode | Gets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.) |
|
Events | Gets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.) |
|
IsInitialized | Gets a value that indicates whether the component is initialized. |
|
IsOpen | Gets a value that indicates whether the data source is currently open and projecting views of data on the DataTable. |
|
Item | Gets a row of data from a specified table. |
|
RowFilter | Gets or sets the expression used to filter which rows are viewed in the DataView. |
|
RowStateFilter | Gets or sets the row state filter used in the DataView. |
|
Site | Gets or sets the site of the component. (Inherited from MarshalByValueComponent.) |
|
Sort | Gets or sets the sort column or columns, and sort order for the DataView. |
|
Table | Gets or sets the source DataTable. |
| Name | Description | |
|---|---|---|
|
AddNew | Adds a new row to the DataView. |
|
BeginInit | Starts the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime. |
|
Close | Closes the DataView. |
|
ColumnCollectionChanged | Occurs after a DataColumnCollection has been changed successfully. |
|
CopyTo | Copies items into an array. Only for Web Forms Interfaces. |
|
Delete | Deletes a row at the specified index. |
|
Dispose() | Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.) |
|
Dispose(Boolean) | Disposes of the resources (other than memory) used by the DataView object. (Overrides MarshalByValueComponent.Dispose(Boolean).) |
|
EndInit | Ends the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime. |
|
Equals(DataView) | Determines whether the specified DataView instances are considered equal. |
|
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
|
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.) |
|
Find(Object) | Finds a row in the DataView by the specified sort key value. |
|
Find(Object[]) | Finds a row in the DataView by the specified sort key values. |
|
FindRows(Object) | Returns an array of DataRowView objects whose columns match the specified sort key value. |
|
FindRows(Object[]) | Returns an array of DataRowView objects whose columns match the specified sort key value. |
|
GetEnumerator | Gets an enumerator for this DataView. |
|
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
|
GetService | Gets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.) |
|
GetType | Gets the Type of the current instance. (Inherited from Object.) |
|
IndexListChanged | Occurs after a DataView has been changed successfully. |
|
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
|
OnListChanged | Raises the ListChanged event. |
|
Open | Opens a DataView. |
|
Reset | Reserved for internal use only. |
|
ToString | Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from MarshalByValueComponent.) |
|
ToTable() | Creates and returns a new DataTable based on rows in an existing DataView. |
|
ToTable(String) | Creates and returns a new DataTable based on rows in an existing DataView. |
|
ToTable(Boolean, String[]) | Creates and returns a new DataTable based on rows in an existing DataView. |
|
ToTable(String, Boolean, String[]) | Creates and returns a new DataTable based on rows in an existing DataView. |
|
UpdateIndex() | Reserved for internal use only. |
|
UpdateIndex(Boolean) | Reserved for internal use only. |
| Name | Description | |
|---|---|---|
|
Disposed | Adds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.) |
|
Initialized | Occurs when initialization of the DataView is completed. |
|
ListChanged | Occurs when the list managed by the DataView changes. |
| Name | Description | |
|---|---|---|
|
AsParallel | Enables parallelization of a query. (Defined by ParallelEnumerable.) |
|
AsQueryable | Converts an IEnumerable to an IQueryable. (Defined by Queryable.) |
|
Cast<TResult> | Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.) |
|
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.
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;
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.
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
- 1/30/2012
- Luvy Gonzalez
- 1/30/2012
- Luvy Gonzalez
Note