DataTable search functionality VB.Net



DataTable Select. A DataTable stores rows and columns of certain types of data. It contains other data—it is a collection. But the DataTable also provides search functionality.

With the Select Function, we query a DataTable for rows that match a condition. We can then loop over the resulting array of DataRow objects.

Module Module1

    Sub Main()
        ' Create a table.
        Dim table As DataTable = New DataTable("Csharp")

        ' Add 2 columns.
        table.Columns.Add(New DataColumn("Size", GetType(Integer)))
        table.Columns.Add(New DataColumn("Team", GetType(Char)))

        ' Add 5 rows.
        table.Rows.Add(100, "a"c)
        table.Rows.Add(235, "a"c)
        table.Rows.Add(250, "b"c)
        table.Rows.Add(310, "b"c)
        table.Rows.Add(150, "b"c)

        ' Get Csharp above 230 size with "b" team.
        Dim result() As DataRow = table.Select("Size >= 230 AND Team = 'b'")

        ' Loop and display.
        For Each row As DataRow In result
            Console.WriteLine("{0}, {1}", row(0), row(1))
        Next
    End Sub

End Module
Module Module1

    Sub Main()
        ' Create a table.
        Dim table As DataTable = New DataTable("Widgets")

        ' Add 2 columns.
        table.Columns.Add(New DataColumn("ID", GetType(Integer)))
        table.Columns.Add(New DataColumn("Date", GetType(DateTime)))

        ' Add rows.
        table.Rows.Add(100, New DateTime(2001, 1, 1))
        table.Rows.Add(200, New DateTime(2002, 1, 1))
        table.Rows.Add(300, New DateTime(2003, 1, 1))

        ' Get rows more recent than 6/1/2001.
        Dim result() As DataRow = table.Select("Date > #6/1/2001#")

        ' Loop.
        For Each row As DataRow In result
            Console.WriteLine(row(0))
        Next
    End Sub

End Module

0 Comment's

Comment Form