Select a Subset of Data from a DataSet

Problem

You want to find specific rows in a DataSet.

Solution

Use the DataTable.Select method or the DataRowCollection.Find method.

Discussion

You can use several approaches to find individual DataRow objects in a DataSet. The most obvious approach is to simply iterate through the collection of DataRow objects until you find one that matches the criteria you are looking for.

Dim MatchedRows() As New ArrayList

Dim Row As DataRow
For Each Row In Ds.Tables(0).Rows
    If Row(FieldName) = FieldValue Then
        MatchedRows.Add(Row)
    End If
Next

However, ADO.NET also provides two more-convenient approaches. First, you can use the DataRowCollection.Find method to find a single row based on its unique primary key. For example, if ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.