O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required