Saturday, November 5, 2011

How to use DataView in Visual Basic .Net


A DataView is an object that allows you to create multiple views of your data and that can be used for data binding. The concept is fairly simple, but its importance cannot be overstated. The flexibility it introduces is impressive because a DataView does not actually change the DataTable from which it is generated. Instead, it offers a filtered window to the data. The following code shows how you can use the DataView control to filter rows based on a column value (in this case, the FirstName and LastName columns):
Function TestDataView()
   Dim adapter As New OleDbDataAdapter("Select * From Customers", _
   connStr)
   Dim ds As New DataSet()

   adapter.Fill(ds)

   Dim view1 As DataView = ds.Tables("Customers").DefaultView
   Dim view2 As DataView = ds.Tables("Customers").DefaultView

   view1.RowFilter = "'LastName' Like 'O%'"
   view2.RowFilter = "'FirstName' Like 'E%'"

   Dim i As Integer

   Debug.WriteLine("All LastNames starting with 'O'")
   For i = 0 To view1.Count - 1
      Debug.WriteLine(view1(i)("LastName"))
   Next

   Debug.WriteLine("All FirstNames starting with 'E'")
   For i = 0 To view2.Count - 1
      Debug.WriteLine(view1(i)("FirstName"))
   Next
End Function

No comments:

Post a Comment