Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 924
Phil Avatar asked Jun 17 '10 08:06

Phil


2 Answers

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;
like image 88
codingbadger Avatar answered Sep 20 '22 09:09

codingbadger


Junt in case... Think you've got a small typo in your VB.NET code. It should be dv.RowFilter instead of dv.RowStateFilter, so:

Dim dt As New DataTable
Dim dv As New DataView(dt)
dv.RowFilter = "GroupingID = 0"
DataGridView1.DataSource = dv
like image 25
Sérgio Terenas Avatar answered Sep 24 '22 09:09

Sérgio Terenas