Search code examples
asp.netvb.netfiltersqldataadapterdatatable.select

How to correctly filter a datatable (datatable.select)


Dim dt As New DataTable
Dim da As New SqlDataAdapter(s, c)

        c.Open()
        If Not IsNothing(da) Then
            da.Fill(dt)
            dt.Select("GroupingID = 0")
        End If

        GridView1.DataSource = dt
        GridView1.DataBind()
        c.Close()

When I call da.fill I am inserting all records from my query. I was then hoping to filter them to display only those where the GroupingID is equal to 0. When I run the above code. I am presented with all the data, the filter did not work. Please can you tell me how to get this working correctly. Thanks.


Solution

  • dt.Select() returns an array of DataRows.

    Why don't you use a DataView?

     DataView dv = new DataView(dt);
     dv.RowFilter = "GroupingID = 0";
     GridView1.DataSource = dv;