The dataview class is used to represent the views of a custom datatable. The relationship between able and dataview follows the famous design pattern-document/view pattern, where datatable is a document and dataview is a view.
At any time, you can have multiple views based on the same data. More importantly, you can process each view with its own set of attributes, methods, and events as an independent object. This also represents a huge leap over ADO.
Create dataview
public DataView();public DataView(DataTable);
Dataview is available only when it is connected to an existing, possibly non-empty able object. Generally, this connection is specified during construction.
DataView dv;dv = new DataView(theDataSet.Tables["Employees"]);
However, you can create a new view and associate it with the table by using the table attribute.
DataView dv = new DataView();dv.Table = theDataSet.Tables["Employees"];
The dataview constructor allows you to get a dataview object from the able. If needed, or vice versa. In fact, the defaultview attribute of the datatable object returns a dataview object for the table.
Dataview DV = DT. defaultview;
Once you have a dataview object, you can use its attributes to create a data row set that you want users to see. Generally, you can use the following attributes:
The former allows you to customize rules for matching visible data in the view. The latter uses expressions for sorting. Of course, you can use any combination of the two.
Rowfilter is a read/write attribute used to read and set table filter expressions.
public virtual string RowFilter {get; set;}
You can use any legal combination of column names, logical and numeric operators, and constants to form an expression. Here are some examples:
dv.RowFilter = "Country = 'USA'";dv.RowFilter = "EmployeeID >5 AND Birthdate < #1/31/82#"dv.RowFilter = "Description LIKE '*product*'"
Let's take a look at the basic filter rules and operators.
The filter string is the logical connection of the expression. You can use and, or, not to connect to a short expression, or you can use parentheses to form a clause to specify a priority operation.
The clauses that usually contain column names are compared with letters, numbers, dates, or other column names. Here, you can use Relational operators and arithmetic operators, such as >=, <,>, +, *, % (Modulo), and so on.
If the row to be selected cannot be easily expressed by arithmetic or logical operators, you can use the in operator. The following code selects a random row:
dv.RowFilter = "employeeID IN (2,4,5)"
You can also use wildcards * and %, which are more useful when used together with the like operator. They all represent any number of characters and can be used with each other.
Note that if the like clause already contains * or % characters, you must enclose it in square brackets to avoid ambiguity. If, unfortunately, the Chinese brackets of the string also exist, they must also be enclosed. In this way, the matching statement is as follows:
dv.RowFilter = "Description LIKE '[[]*[]]product[[]*[]]"
Wildcard characters can only be used at the beginning or end of a filter string, but cannot appear in the middle of a string. For example, the following statement produces a runtime error:
dv.RowFilter = "Description LIKE 'prod*ct"
The string must start with single quotes, and the date type must start with the # symbol. Numeric values can use decimal points and scientific notation.
Rowfilter also supports aggregate functions, such as sum, Count, Min, Max, and AVG. If there are no data rows in the table, the function returns NULL.
At the end of introducing the rowfilter expression, let's discuss three convenient functions: Len, IIF, and substring.
As its name, Len () returns the length of a specific expression. This expression can be a column name or another legal expression.
Substring () returns the character substring of a specified expression starting from a specific position.
I like IIF () most, which has one or two values according to the logical expression value. IIF is a compact expression of the if-then-else statement. Syntax:
IIF(expression, if_true, if_false)
Through this function, you can create a very complex filter string. For example, assume that you obtain the Employees table from the SQL server's northwind database, the following expressions can be used to select employees whose employeeid is less than 6 and whose lastname is an even number of characters and whose employeeid is greater than 6 and whose lastname is an odd number of characters.
IIF(employeeID<6, Len(lastname) %2 =0, Len(lastname) %2 >0)
Pre-arranged View
In the preceding example, the DataGrid must be used to pre-arrange data rows in the view to refresh the user interface. The automatic mechanism is. net.
The product of data binding. The DataGrid is a data binding control that obtains data through the datasource attribute. Dataview is a data binding class that can be used to construct the content of the datasource attribute.
What should you do if you want to use another control other than the DataGrid? What if you don't want to use automatic data binding? How should we pre-arrange the selected data rows in the view?
The table attribute of dataview points to the corresponding data table, but the datatable does not save the filtering information. Therefore, the data in the pre-arranged table is destined to be unfeasible. Although datatable and dataview are closely related, they are independent and execute independent functions.
The following Visual Basic. Net code snippet shows how to traverse all data rows in the view and add them to ListBox.
Dim dv As New DataView()dv = ds.Tables("Employees").DefaultViewdv.RowFilter = "employeeid >5"ListBox1.Items.Clear()Dim buf As StringDim dr As DataRowViewFor Each dr In dvbuf = ""buf &= dr("lastname").ToString()& ", " & dr("firstName").ToString()ListBox1.Items.Add(buf)Next
As mentioned above, dataview is an enumeration class, so you can safely pass it to the for... each statement. The Count attribute stores the number of rows in the view for use in the for... next loop.
To access a row in the view, you can use the datarowview class. Datarowview can be used to represent datarow views, just as dataview expresses datatable custom views.
In general, datarow can have up to four States: Default, original, current, and proposed. These statuses are set by the datarowversion Enumeration type and expressed by the rowversion attribute.
Datarow views can only be in one of the states.
The default version of a data row is available only when the default value is set during column construction. The initial (original) version means that after the acceptchanges of the last called table, the data row or snapshot is obtained from the number drama source. The current version refers to the current data row, including all updates that occurred at the time. The proposed status only exists when you call beginedit and endedit.
Datarowview can be accessed using the same datarow syntax. The most important attribute here is item.
Sorting and other convenient features
Dataview supports the sort attribute and can be used to sort the content in the view. Sort is sorted by column name expressions separated by commas. By adding ASC or DESC restrictions to any column name, you can arrange the fields in the ascending or descending order. If no direction limiting word exists, the default order is ASC.
Dataview is an object in memory, so it is sorted locally without calling the database server.
Rowstatefilter is another interesting attribute of dataview. It can use any predefined criteria to filter the content in the datatable. The following table lists all values of the dataviewrowstate Enumeration type:
Currentrows |
Includes all unupdated, new, and modified data rows |
Deleted |
All data rows deleted after the last acceptchanges call |
Modifiedcurrent |
All data rows modified since the last call of acceptchanges |
Modifiedoriginal |
All data lines of the original version after the last acceptchanges call |
New |
All newly added rows since the last acceptchanges call |
Originalrows |
Returns the initial data row, including the unchanged and deleted |
Unchanged |
All unupdated data rows |
If you want to operate on non-connected data, all updates take effect after you call acceptchanges for the datatable. Updates to a single row take effect after the datarow acceptchanges is called. Similarly, these updates can be canceled by calling the rejectchanges of the able or datarow object.
The dataview object also has some attributes, such as allowedit, allowdelete, and allownew, to get or set whether to allow update. They are set to true by default and allow updates of any type. If you want to complete the update operation when the flag is set to false, a runtime error will occur.
Dataviewmanager class
The defaultview attribute of the datatable object is used to return a dataview object, which is used as the default view of the content in the data table. It reads data in a natural order and displays all rows in the table without any filtering.
theMasterGrid.DataSource = m_ds.Tables("Employees").DefaultView
If you need data-specific views, you can sort and/or filter defaview view objects directly.
m_ds.Tables("Employees").DefaultView.Sort = "lastname"theMasterGrid.DataSource = m_ds.Tables("Employees").DefaultView
The dataviewmanager class is used to store view settings for all tables in the dataset.
You can create a dataviewmanager by passing a valid non-empty dataset to the class constructor.
Dim dvm As DataViewManagerdvm = New DataViewManager(m_ds)
You can also obtain the following information through the defaultviewmanager attribute of the DataSet object:
Dim dvm As DataViewManager = m_ds.DefaultViewManager
The important thing is that the dataviewmanager class is associated with the same dataset. The following is another feasible method:
Dim dvm As New DataViewManager()dvm.DataSet = m_ds
The most important attribute of dataviewmanager is dataviewsettings, a set of dataviewsetting objects.
Dim dvs As DataViewSettingdvs = dvm.DataViewSettings("Employees")dvs.Sort = "lastname"
The dataviewsetting object contains the parameter information of the table view. When you bind data to a data-sensitive control, you can use dataviewmanager instead of dataset or datatable to retain your view settings (filter and sort fields)
theMasterGrid.DataSource = dvmtheMasterGrid.DataMember = "Employees"
Here, the view Automatically sorts and filters the Employees table specified by dataviewsetting. In other words, the dataviewsetting class is a cache for the view of a specific table.