![]() ![]() | ||
We've worked with datasets primarily in this chapter, but, as discussed in the In Depth section, you also can use data views to get a snapshot of the data in a table and work with it. Data views are much like read-only mini-datasets; you typically load only a subset of a dataset into a data view.
There's an example named DataViews on the CD-ROM. In that example, I've connected an SQL data adapter to the authors table in the SQL Server pubs database, and created a dataset, DataSet11, from that table. Next, I add a data view, DataView1, to this example, by clicking the Data tab in the toolbox and dragging a data view to the example's main form. To specify where the data view should get its data from, I set the data view's Table property to DataSet11.authors. (When you click the Table property in the Properties window, Visual Basic will list the available tables to work with.)
Next, I add a data grid to the main form, and set the data grid's DataSource property to DataView1 (without setting the data grid's DataMember property). This connects the data grid to the data view, and if we did nothing more, the data grid would pass the entire authors table on to the data grid. However, you usually use data views to work with just a subset of the data in a table, and you can specify what subset you want using the RowFilter property. For example, to get only authors with the last name White, I'll set the data view's RowFilter property to au_lname = "White". And, as usual, I add code to populate the data set from a data adapter:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load DataSet11.Clear() SqlDataAdapter1.Fill(DataSet11) End Sub
You can see the results in Figure 20.31, where the only author whose last name is White is displayed. In this way, we've loaded a filtered snapshot of our data into a data view and bound it to a data grid.
You also can use a data view's DataViewRowState property to add rows to a data view depending on their state. For example, you can place rows that have been deleted or are new in a data view using this property. Here are the possible state values you can use, and the types of rows they match, from the DataViewRowState enumeration:
Added—Added rows.
CurrentRows—Current rows including unchanged, new, and modified rows.
Deleted—Deleted rows.
ModifiedCurrent—The current rows, a modified version of the original data.
ModifiedOriginal—The original rows which have since been modified; the modified rows are available in ModifiedCurrent.
None—No rows.
OriginalRows—Original rows, including unchanged and deleted rows.
Unchanged—Unchanged rows.
For example, here's how I make sure that only rows that have been added to a table appear in a data view:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load DataSet11.Clear() SqlDataAdapter1.Fill(DataSet11) DataView1.RowStateFilter = DataViewRowState.Added End Sub
![]() ![]() | ||