Operate. Net ApplicationsProgramThe dataset class is often used for data, but developers often underestimate the dataview class, which provides the sorting and filtering functions. When using dataset, these operations must be called using SQL. Now let's take a closer look at why you want to use the dataview class.
Where to get it
The class libraries of the. NET Framework are irresistible. Sometimes it takes some effort to find a specific project. The dataview class is located in the system. Data namespace. Therefore, a dataview instance can be declared using the following lines:
Dim DV as system. Data. dataview 'vb. net
System. Data. dataviewdv = NULL; // C #
You can also add the namespace to the project to avoid entering the complete namespace path, as shown below:
Imports system. Data
Dim DV as dataview
The dataset class is located in the same namespace. Before discussing dataview, let's take a look at the dataset class.
A quick journey to Dataset
Dataset classes are often used when operating external data sources, and data must be used in applications. Dataset receives and uses data for programmers. It provides methods and attributes for accessing a single table (as well as data rows in the table.
The following VB. NETCodeDataset is filled with data from the product table in the SQL server's northwind database. It displays some results in an HTML table. This table is displayed when the page is requested.
Sub page_load ()
Dim DV as system. Data. dataview
Dim ds as system. Data. Dataset
Dim da as system. Data. sqlclient. sqldataadapter
Dim row as system. Data. datarow
Dim strsql as string, connstring as string
Connstring = "Data Source = dev; uid = test; Pwd = test"
Strsql = "select * From northwind. DBO. Products"
DA = new system. Data. sqlclient. sqldataadapter (strsql, connstring)
DS = new system. Data. dataset ()
Da. Fill (DS)
Response. Write ("<Table> ")
For each row in DS. Tables (0). Rows
Response. Write ("<tr> <TD> ")
Response. Write (row (0 ))
Response. Write ("</TD> <TD> ")
Response. Write (row (1 ))
Response. Write ("</TD> </tr> ")
Next row
Response. Write ("</table> ")
End sub
This type of solution can be used in many cases, but when data needs to be sorted or filtered in some way, the problem will occur. Sorting or filtering can be easily changed by changing the SQL statement, but this requires more interactive operations with the database server. The dataview class provides a simple and intuitive way to operate data.
Dataview to save fire
Changing the SQL statement may become boring, and cannot be implemented even in certain application designs. The dataview class uses its rowfilter method to provide a much clearer method. This method allows the underlying data to be filtered Based on the name and value of the data column. The syntax is as follows:
Dataview. rowfilter = "[column_name] = 'value '"
The value format is determined by the format of the data column, so no number is required between the apostrophes. This is much easier than changing the SQL statement to include a where clause and submit the SQL statement to the database server. Before providing the sample code, we need to quickly review the sort method.
The sort method of the dataview class allows the underlying data to be easily sorted in a specific data column. The name of the data column is passed as a string value, as shown below:
Dataview. Sort = "columnname"
This is similar to the SQL order by clause, but it does not need to be called by the database. The sort method will quickly sort the underlying data so that it can be used as required. The following example uses both methods to manipulate data from the northwind database. All the contents of the database are used to fill in a DataSet object (through the tables attribute ). In this regard, the dataview object is manipulated using the rowfilter and sort methods. The count method is used to display the number of records before and after the application filter. Once records are filtered and sorted, they are displayed in an HTML table. The datarowview object is used to access a separate dataview data row (and then access columns ).
<Script language = "VB" runat = "server">
Sub page_load ()
Dim DV as system. Data. dataview
Dim ds as system. Data. Dataset
Dim da as system. Data. sqlclient. sqldataadapter
Dim row as system. Data. datarow
Dim strsql as string, connstring as string
Connstring = "Data Source = dev; uid = test; Pwd = test"
Strsql = "select productid, productname from northwind. DBO. Products"
DA = new system. Data. sqlclient. sqldataadapter (strsql, connstring)
DS = new system. Data. dataset ()
Da. Fill (DS)
Response. Write ("<Table> ")
DV = new system. Data. dataview (Ds. Tables (0 ))
Response. Write ("before count:" & CSTR (DV. Count () & "<br> ")
DV. rowfilter = "[productid] = 4"
DV. Sort = "productname"
Response. Write ("after count:" & CSTR (DV. Count () & "<br> ")
Dim X as integer, y as integer
Dim DRV as system. Data. datarowview
Response. Write ("<Table> ")
For x = 0 to (DV. Count-1)
DRV = DV. Item (X)
Response. Write ("<tr> ")
For y = 0 to (DRV. Row. itemarray. getlength (0)-1)
Response. Write ("<TD>" + CSTR (DRV. Row. Item (y) + "</TD> ")
Next y
Response. Write ("</tr> ")
Next x
Response. Write ("</table> ")
End sub
</SCRIPT>
This example is simple, but fully shows the flexibility provided by the dataview class.
Let it serve you
Data Filtering and sorting are standard components of most applications, so it is advantageous to use dataview. This topic (although only) explains the concept of dataview, but you can combine it with the DataGrid web control to make the dataview class more powerful applications. The dataview class can be used as the underlying data source of the DataGrid, which simplifies sorting and filtering operations.
Author: Tony Patton started his career as an application developer and has obtained Java, VB, Lotus, and XML certifications to increase his expertise.