With the help of DataView, we can create different views for the data stored in the DataTable. For example, through DataView, you can use different sort orders to view the data in the DataTable, or filter the data in the DataTable based on data column status or filter expressions. The important thing is that when we need to create different views for the data in the DataTable and bind these data to controls on the form, we need to use DataView to complete it.
DataView provides a dynamic data view, that is, its content, sort order, and members will reflect any changes in the source DataTable. Obviously the DataView is very different from the DataTable.Select method. The Select method will return an array of DataRow objects from the data table based on a specific filter expression or sort order, and its members and sort order are static. Because of the dynamic reactive nature of DataView, it is very suitable for use in data binding applications.
DataView provides you with a dynamic view of a single data collection. You can apply different sort orders and filter conditions to the data collection. This is somewhat similar to the View provided by the SQL Server database. However, there is still a big difference between DataView and database view, because DataView cannot be used as a data table, nor can it provide a view to connect the data table. In addition, not only can you not exclude fields in the source data table, you cannot also add additional fields that do not exist in the source data table (for example: expression fields).
You can create a DataView in two ways. The first way is to use the DataView constructor, and the second way is to create a reference to the DefaultView property of the DataTable. This article will explore in detail how to use these two methods to create DataView.
Using the DataView constructor
The DataView constructor provides three multiload versions as shown in the table below.
Multiload version of DataView constructor
DataView()
DataView(ByVal table As DataTable)
DataView(ByVal table As DataTable, _
ByVal RowFilter As String, _
ByVal Sort As String, _
ByVal RowState As DataViewRowState)
The first version of the DataView constructor initializes a new instance of the DataView class without any parameters. Please note that if you use this version to create a DataView, you must first set the Table property after creating the DataView object to determine its source DataTable, and then you can continue to set other properties (RowFilter, Sort...etc.).
The following program code demonstrates how to use the first version of the DataView constructor to create a DataView object in order to filter and sort the data columns of the "Zhang Limin Studio" data table in the data set, and bind the DataGridView control to this DataView. We found that the DataGrid control will only display women, and the data will be arranged in the order of the strokes of the names from most to least. The following is the program code for this example:
SqlDataAdapter1.Fill(Ds Zhang Limin Studio, "Zhang Limin Studio")
'Create DataView object
Dim dv As DataView = New DataView
' Since the DataView constructor without any parameters is used to create the DataView object,
' Therefore, the Table property must be set first to determine its source DataTable
dv.Table = Ds Zhang Limin Studio. Zhang Limin Studio
' Set the sort order so that the order of the strokes of the name is arranged from most to least.
dv.Sort = "NameDESC"
' Set filter conditions to display only women
dv.RowFilter = "Gender = 'Female'"
' Bind the DataGridView control to the DataView
DataGridView1.DataSource = dv
The second version of the DataView constructor represents using the specified DataTable to initialize a new instance of the DataView class.
The following program code demonstrates how to use the second version of the DataView constructor to create a DataView object in order to filter and sort the data columns of the "Zhang Limin Studio" data table in the data set, and bind the DataGridView control to this DataView . We found that the DataGridView control will only display the employee data of the "Information Department", and the data will be arranged from high to low according to the current salary:
SqlDataAdapter1.Fill(Ds Zhang Limin Studio, "Zhang Limin Studio")
' Create a DataView object
Dim dv As DataView = New DataView(Ds Zhang Limin Studio. Zhang Limin Studio)
'Set the sort order to sort from high to low based on current salary
dv.Sort = "Current Salary DESC"
'Set filter conditions to display only employee data of "Information Department"
dv.RowFilter = "Department = 'Information Department'"
' Bind the DataGridView control to the DataView
Me.DataGridView1.DataSource = dv
The third version of the DataView constructor represents initializing a new instance of the DataView class using the specified DataTable, RowFilter, Sort, and DataViewRowState.
The following program code demonstrates how to use the third version of the DataView constructor to create a DataView object in order to filter and sort the data columns of the "Zhang Limin Studio" data table in the data set, and bind the DataGridView control to this DataView . We found that the DataGridView control will only display employee data whose current salary is greater than 49,000 yuan, and the data will be arranged from high to low according to the current salary:
SqlDataAdapter1.Fill(Ds Zhang Limin Studio, "Zhang Limin Studio")
' Create a DataView object
Dim dv As DataView = New DataView( _
Ds Zhang Limin Studio. Zhang Limin Studio, _
"Current salary > 49000", _
"Current salary DESC", _
DataViewRowState.CurrentRows)
' Bind the DataGridView control to the DataView
Me.DataGridView1.DataSource = dv
I would like to remind everyone that when the DataView is created, and when any of the Sort, RowFilter or RowStateFilter properties are modified, the DataView's index will be re-established. This means that if you want to enjoy the best performance, you should specify the sort order or filter criteria directly in the constructor when creating the DataView. If you do not directly specify the sort order or filter conditions in the constructor when creating a DataView, but instead set the Sort, RowFilter or RowStateFilter properties of the DataView object after creating it, the index of the DataView will be re-established. Causes the index to be created at least twice.
Using the DefaultView property of DataTable
The DefaultView property of the DataTable returns a DataView object that uses the DataTable as the source data table, allowing you to sort, filter, and search the data columns in the DataTable. If the DataView you create wants to display all the data columns in the DataTable and arrange them in natural order, using the DefaultView property of the DataTable will be a very direct and convenient way to create the DataView.
For the program whose execution screen is shown in Figure 1, it uses the DefaultView property of the DataTable to create a DataView so that all the data columns of the source data table can be displayed in the DataGrid control at the beginning, and allows the user to pass the DataView during the execution phase. to dynamically filter data. The program code is listed below:
' Category level declaration of DataView object
Private dv As DataView
Private Sub DemoForm5_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
FillComboBoxDepartment()
SqlDataAdapter1.Fill(Ds Zhang Limin Studio, "Zhang Limin Studio")
' Create DataView
dv = Ds Zhang Limin Studio. Zhang Limin Studio.DefaultView
' Display the number of data columns in the DataView
txtRowCount.Text = dv.Count.ToString
' Bind the DataGridView control to the DataView
Me.DataGridView1.DataSource = dv
End Sub
Private Sub FillComboBoxDepartment()
'Create a data command object (ie SqlCommand object)
Dim foxCMD As New SqlCommand
foxCMD.Connection = SqlConnection1
foxCMD.CommandText = "SELECT DISTINCT Department FROM dbo.Zhang Limin Studio"
'Open the connection
SqlConnection1.Open()
Using myReader As SqlDataReader = foxCMD.ExecuteReader()
If myReader.HasRows Then
While myReader.Read()
ComboBoxDepartment.Items.Add(myReader.GetSqlString(0))
End While
End If
End Using
ComboBoxDepartment.SelectedIndex = 0
End Sub
Private Sub btnFilter_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFilter.Click
dv.RowFilter = "Department= '" & _
ComboBoxDepartment.SelectedItem.ToString() & "'"
'Display the number of data columns in the DataView
txtRowCount.Text = dv.Count.ToString
End Sub
http://www.cnblogs.com/liminzhang/archive/2006/10/23/537518.html