Imports System.Text
Public Class DataTest
Public Sub TestView()
Dim ds As New DataSet()
Dim dt As New DataTablePlus()
ds.Tables.Add(dt)
dt.Columns.Add(New DataColumn("FirstName"))
dt.Columns.Add(New DataColumn("LastName"))
Dim col As ArrayList
col = GetPeople()
Dim i As Integer
For i = 0 To col.Count - 1
Dim dr As DataRow
dr = dt.NewRow()
dr.Item("FirstName") = DirectCast(col.Item(i), Person).FirstName
dr.Item("LastName") = DirectCast(col.Item(i), Person).LastName
dt.Rows.Add(dr)
Next
Dim vew0 As New DataViewPlus()
vew0.Name = "All"
vew0.Table = dt
vew0.RowFilter = ""
dt.Views.Add(vew0)
Dim vew1 As New DataViewPlus()
vew1.Name = "Georges"
vew1.Table = dt
vew1.RowFilter = "FirstName=George"
dt.Views.Add(vew1)
Dim vew2 As New DataViewPlus()
vew2.Name = "Thomases"
vew2.Table = dt
vew2.RowFilter = "FirstName=Thomas"
dt.Views.Add(vew2)
PrintView(dt.Views.Item("All"))
PrintView(dt.Views.Item("Georges"))
PrintView(dt.Views.Item("Thomases"))
End Sub
Private Function GetPeople() As ArrayList
Dim col As New ArrayList()
col.Add(New Person("John", "Adams"))
col.Add(New Person("Benjamin", "Franklin"))
col.Add(New Person("George", "Washington"))
col.Add(New Person("Thomas", "Jefferson"))
col.Add(New Person("James", "Madison"))
col.Add(New Person("Alexander", "Hamilton"))
col.Add(New Person("George", "Mason"))
Return col
End Function
Private Sub PrintView(ByVal vw As DataViewPlus)
Dim row As DataRow
Dim i, n As Integer
Dim sb As New StringBuilder()
For i = 0 To vw.Count - 1
For n = 0 To vw.Table.Columns.Count - 1
sb.Append(vw.Table.Columns.Item(n).ColumnName)
sb.Append("=")
sb.Append(vw.Item(i).Item(n))
sb.Append(" : ")
Next
sb.Append(vbCrLf)
Next
MsgBox(sb.ToString())
End Sub
Private Class Person
Public FirstName As String
Public LastName As String
Public Sub New(ByVal fname As String, ByVal lname As String)
FirstName = fname
LastName = lname
End Sub
End Class
End Class