DataView


A DataView object represents a customizable view of the data contained in a DataTable. You can use the DataView to select some or all of the DataTable’s data and display it sorted in some manner without affecting the underlying DataTable.

A program can use multiple DataViews to select and order a table’s data in different ways. You can then bind the DataViews to controls such as the DataGrid control to display the different views. If any of the views modifies its data, for example by adding or deleting a row, the underlying DataTable object’s data is updated and any other views that need to see the change are updated as well.

The following code builds a DataTable named Contacts containing the fields FirstName, LastName, Street, City, State, and Zip. It places a uniqueness constraint on the FirstName/LastName pair and adds some rows of data to the table. It then binds the DataTable to the DataGrid control named grdAll. Next the program makes a DataView named dv_co based on the table, sets its RowFilter property to make it select rows where the State field has the value CO, and binds the DataView to the DataGrid named grdCO. Finally, the code makes another DataView with RowFilter set to select records where the FirstName field is greater than or equal to E and binds that DataView to the grdName DataGrid.

  Private Sub Form1_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     ' Make a DataTable.     Dim contacts_table As New DataTable("Contacts")     ' Add columns.     contacts_table.Columns.Add("FirstName", GetType(String))     contacts_table.Columns.Add("LastName", GetType(String))     contacts_table.Columns.Add("Street", GetType(String))     contacts_table.Columns.Add("City", GetType(String))     contacts_table.Columns.Add("State", GetType(String))     contacts_table.Columns.Add("Zip", GetType(String))     ' Make the combined FirstName/LastName unique.     Dim first_last_columns() As DataColumn = { _         contacts_table.Columns("FirstName"), _         contacts_table.Columns("LastName") _     }     contacts_table.Constraints.Add( _         New UniqueConstraint(first_last_columns))     ' Make some contact data.     contacts_table.Rows.Add(New Object() {"Art", "Ant", _         "1234 Ash Pl", "Bugville", "CO", "11111"})     contacts_table.Rows.Add(New Object() {"Bev", "Bug", _         "22 Beach St", "Bugville", "CO", "22222"})     contacts_table.Rows.Add(New Object() {"Cid", "Cat", _         "3 Road Place Lane", "Programmeria", "KS", "33333"})     contacts_table.Rows.Add(New Object() {"Deb", "Dove", _         "414 Debugger Way", "Programmeria", "KS", "44444"})     contacts_table.Rows.Add(New Object() {"Ed", "Eager", _         "5746 Elm Blvd", "Bugville", "CO", "55555"})     contacts_table.Rows.Add(New Object() {"Fran", "Fix", _         "647 Foxglove Ct", "Bugville", "CO", "66666"})     contacts_table.Rows.Add(New Object() {"Gus", "Gantry", _         "71762-B Gooseberry Ave", "Programmeria", "KS", "77777"})     contacts_table.Rows.Add(New Object() {"Hil", "Harris", _         "828 Hurryup St", "Programmeria", "KS", "88888"})     ' Attach grdAll to the DataTable.     grdAll.DataSource = contacts_table     grdAll.CaptionText = "All Records"     ' Make a DataView for State = CO.     Dim dv_co As New DataView(contacts_table)     dv_co.RowFilter = "State = 'CO'"     grdCO.DataSource = dv_co     grdCO.CaptionText = "CO Records"     ' Make a DataView for FirstName >= E.     Dim dv_name As New DataView(contacts_table)     dv_name.RowFilter = "FirstName >= 'E'"     grdName.DataSource = dv_name     grdName.CaptionText = "LastName >= E" End Sub  

Figure 11-28 shows the result. The DataGrid on the top is bound to the DataTable and shows all the table’s rows. The second DataGrid is bound to the dv_co DataView and displays records where State = CO. The bottom DataGrid is bound to the dv_name DataView, so it displays records where FirstName >= E. If you use any of these DataGrid controls to modify the data, the other grids immediately show the updates.

image from book
Figure 11-28: Different DataView objects can show different views of the same data.

The DataView class is geared more toward data display than toward storage. It basically refers to data stored in a DataTable object, so it doesn’t provide the same features for managing relations and constraints that the DataTable does. It does, however, provide links to the DataRow objects it represents. From those objects, you can get back to the rows’ DataTable objects if necessary.

The following table describes the DataView object’s most useful properties.

Open table as spreadsheet

Property

Purpose

AllowDelete

Determines whether the DataView allows row deletion. If this is False, any bound controls such as the DataGrid will not allow the user to delete rows.

AllowEdit

Determines whether the DataView allows row editing. If this is False, any bound controls (such as the DataGrid) will not allow the user to edit rows.

AllowNew

Determines whether the DataView allows new rows. If this is False, any bound controls (such as the DataGrid) will not allow the user to add rows.

Count

Returns the number of rows selected by the view.

Item

Returns a DataRowView object representing a row in the view.

RowFilter

A string that determines the records selected by the view.

RowStateFilter

The state of the records that should be selected by the view. This can be Added, CurrentRows (unchanged, new, and modified rows), Deleted, ModifiedCurrent (current version of modified rows), ModifiedOriginal (original version of modified rows), None, OriginalRows (original, unchanged, and deleted rows), and Unchanged.

Sort

A string giving the columns that should be used to sort the data. For example, “State, City, Zip” sorts by State, then City, and then Zip in descending order.

Table

Specifies the underlying DataTable object.

The following table describes some of the most useful DataView methods.

Open table as spreadsheet

Method

Purpose

AddNew

Adds a new row to the underlying DataTable.

Delete

Deletes the row with a specific index from the underlying DataTable.

Find

Returns the index of a row that matches the view’s sort key columns. This method returns -1 if no row matches the values it is passed. It raises an error if the number of values it receives does not match the number of the DataView's key values.

FindRows

Returns an array of DataRowView objects representing rows that match the view’s sort key columns.

The DataView object’s Sort property determines not only the fields by which the data is sorted but also the key fields used by the Find method. The following code makes the dv_name DataView sort by FirstName and LastName. It then uses the Find method to display the index of a row with FirstName = Hil and LastName = Harris.

  dv_name.Sort = "FirstName, LastName" MessageBox.Show(dv_name.Find(New String() {"Hil", "Harris"}).ToString) 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net