DataView

A DataView is basically similar to a conventional database view, which is just a view of a datatable with different sorting and filtering criteria.
DataViews represent a customised view of a DataTable ideal for sorting, filtering, searching, editing etc


A DataView is similar to a live view on a DataTable, which allows programmers to set a sort order and filter on a view of the table.
This is just a view over an existing DataTable.
This gives you the same filtering and sorting capabilities as the DataTable.Select method.


Creating a DataView Object

Dim objDataView As System.Data.DataView 

objDataView = New System.Data.DataView
objDataView.Table = objDataSet.Tables("Projects")

Filtering Rows

This allows you to specify an expression similar to a SQL WHERE clause.

objDataView.RowFilter = "FirstName = 'Russell'"  
objDataView.RowFilter = "FirstName LIKE 'M*'"
objDataView.RowFilter = "Len(LastName) > 8"

objDataView.RowStateFilter = DataViewRowState.Deleted 

Sorting Rows

objDataView.Sort = "FirstName DESC" 

Adding and Deleting

You can add, delete and modify rows in a DataView by using the same methods you would use with the DataTable.


Total Rows

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

objDataView.Count 

Finding Data

You can also use the Find method to retrieve a DataRow given its primary key value.
Both these methods require that a primary key has been defined on the DataTable.
If the primary key exists then a DataRow object is returned. If the primary key does not exist then a Null value is returned.

objDataRow = objDataTable.Rows.Find(100) 

You can also use the Contains method to search for character-based single words or phrases.

bfound = objDataTable.Rows.Contains(100) 

You can also use the Find and FindRows methods of the DataView object.
Both these methods rely on the DataView being sorted by the column entry you are trying to find.

objDataView.Sort = "FirstName" 
iRowIndex = objDataView.Find("Russell")

DataRowView Objects

A DataView contains a collection of DataRowView objects which are views over the rows in the underlying DataTable.

Dim objDataRowView As System.Data.DataRowView 
For Each objDataRowView In objDataTableConsolidation.DefaultView
   sValue = objDataRowView.Item("ColumnName").ToString
Next objDataRowView

DataBinding

This binds the default DataView from a particular table and binds it to a DataGrid control.

Me.dgrDataGrid.DataSource = objDataSet.Tables("TableName").DefaultView 
Me.dgrDataGrid.DataBind().

This obtains the DataView that is associated with a DataGrid.

Dim objDataView As System.Data.DataView 
objDataView = CType(Me.dgrDataGrid.DataSource, System.Data.DataView)

DataViewRowState Properties

CurrentRowsIncludes unchanged, new and modified rows. Deleted rows are not included.
OriginalRowsAll the rows as they were after the most recent AcceptChanges or RejectChanges method. This includes unchanged and deleted rows. Added rows are not included.
UnchangedUnchanged rows only
AddedAdded rows only
DeletedDeleted rows only
ModifiedCurrentChanged rows only. Columns contain the current (modified) value.
ModifiedOriginalChanged rows only. Columns contain the original value.
NoneNo rows are returned.

Properties

AllowDeleteTrue if rows can be deleted.
AllowEditTrue if rows can be modified.
AllowNewTrue if new rows can added
ApplyDefaultSortTrue if the default sort order should be used.
CountReturns the number of rows in this view
DataViewManagerThe DataView associated with this view.
ItemReturns the Nth DataRow
RowFilterAn expression that determines which rows appear in this view
RowStateFilterA DataViewRowState enumerated value that determines how rows are filtered according to their state. It can be None, CurrentRows, OriginalRows, ModifiedCurrent, ModifiedOriginal, Added, Deleted, Unchanged.
SortA string that specifies the column (or columns) used as sort keys.
TableThe source DataTable.
ToTable(Added in 2.0) Allows you to create a new table based on data in the DataView.

Methods

AddNewAdds a new row and returns a DatRowView object that can be used to set field values.
DeleteDeletes the row at the specified index.
FindReturns the row index of a particular row in the DataView given the value of its key column(s). If more than one row matches then the first matching DataRow index is returned. If there are no matches then -1 is returned.

Events

ListChangedFires when the list managed by the DataView changes, that is when an item is added, deleted, moved or modified.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext