DataTable.Select Method (String, String, DataViewRowState)


Gets an array of all DataRow objects that match the filter in the order of the sort that match the specified state.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

Public Function Select (
	filterExpression As String,
	sort As String,
	recordStates As DataViewRowState
) As DataRow()


Type: System.String

The criteria to use to filter the rows. For examples on how to filter rows, see DataView RowFilter Syntax [C#].

Type: System.String

A string specifying the column and sort direction.

Type: System.Data.DataViewRowState

One of the DataViewRowState values.

Return Value

Type: System.Data.DataRow()

An array of DataRow objects.

To form the filterExpression argument, use the same rules for creating the DataColumn class's Expression property value. The Sort argument also uses the same rules for creating class's Expression strings.

If the column on the filter contains a null value, it will not be part of the result.

The following example uses a filter expression and record state to return an array of DataRow objects.

Private Sub GetRowsByFilter()

    Dim customerTable As DataTable = New DataTable("Customers")

    ' Add columns
    customerTable.Columns.Add("id", GetType(Integer))
    customerTable.Columns.Add("name", GetType(String))

    ' Set PrimaryKey
    customerTable.Columns("id").Unique = True
    customerTable.PrimaryKey = New DataColumn() _

    ' add ten rows
    Dim id As Integer
    For id = 1 To 10
        customerTable.Rows.Add( _
        New Object() {id, String.Format("customer{0}", id)})
    Next id

    ' add another ten rows
    For id = 11 To 20
        customerTable.Rows.Add( _
        New Object() {id, String.Format("customer{0}", id)})
    Next id

    Dim expression As String
    Dim sortOrder As String

    expression = "id > 5"
    ' Sort descending by CompanyName column.
    sortOrder = "name DESC"

    ' Use the Select method to find all rows matching the filter.
    Dim foundRows As DataRow() = _
        customerTable.Select(expression, sortOrder, _

    PrintRows(foundRows, "filtered rows")

    foundRows = customerTable.Select()
    PrintRows(foundRows, "all rows")
End Sub

Private Sub PrintRows(ByVal rows() As DataRow, ByVal label As String)
    Console.WriteLine("\n{0}", label)
    If rows.Length <= 0 Then
        Console.WriteLine("no rows found")
        Exit Sub
    End If

    Dim row As DataRow
    Dim column As DataColumn
    For Each row In rows
        For Each column In row.Table.Columns
            Console.Write("\table {0}", row(column))
        Next column
    Next row
End Sub

.NET Framework
Available since 1.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft