filtering a datagrid

jberger

Member
Joined
May 10, 2007
Messages
11
Location
Illinois
Programming Experience
Beginner
I have a form with certain criterion that the user can search by. I'm filling a DataGrid and I'm trying to use DataView.RowFilter to filter the search by the criterion chosen. I've tried to code it different ways, but it keeps populating the DataGrid with all the records in the mdb file. Any help would be appreciated.
 
I do not have the answer for you on that one, but...
ComponentOne makes a datagrid that has a filter bar across the top. The user types in the filter box on any column and the grid is filtered. I and all the users love the functionality.
Give it a look...
 
Thanks, but I already have four seperate textBoxes setup for the user to choose from on how they want to search. I'm just having trouble getting the filter to work properly. Like I said in my first post, when I type in a name from a specific column that I want to filter, it keeps giving me all the records, not just the ones I want.
 
here is a snippet of what I have:
VB.NET:
If BySender.Checked = True Then
    DataFileObj.OleDbConnection2.Open()
    DataFileObj.OleDbSelectCommand1.CommandText = "select * from specifiedTable"
    DataFileObj.OleDbDataAdapter1.Fill(Info_Parcel_Dataset1)
    ds = Info_Parcel_Dataset1
    DataFileObj.OleDbSelectCommand1.CommandText = String.Concat("select * from specifiedTable where sender like '", SenderBox.Text, "%", "'")
    Dim dv As New DataView(ds.Tables("specifiedTable"))
    dv.RowFilter = "Sender LIKE '" & SenderBox.Text & "'"
    DataGrid1.DataSource = dv
End If
Sorry for the way it's displayed. Not quite sure how to get a code window to come up.
 
Last edited by a moderator:
Try this:

VB.NET:
        Dim dv As DataView
        dv = New DataView
        With dv
            .Table = DataSet1.Tables(0)
            .AllowDelete = True
            .AllowEdit = True
            .AllowNew = True
            .RowFilter = "YourColumnName LIKE '%" & TextBox1.Text & "%'"
        End With
        DataGrid1.DataSource = dv

also, and/or operators can be used...as in
VB.NET:
.RowFilter = "YourColumnName1 LIKE '%" & TextBox1.Text & "%' or YourColumnName2 LIKE '%" & TextBox2.Text & "%'"

Hope that helps...
 
Last edited:
I have a form with certain criterion that the user can search by. I'm filling a DataGrid and I'm trying to use DataView.RowFilter to filter the search by the criterion chosen. I've tried to code it different ways, but it keeps populating the DataGrid with all the records in the mdb file. Any help would be appreciated.

The view has to be based on the datatable, and the grid has to be based on the view. You have to load all records from the file for the filter to work.

Note that this is usually quite a poor way to work; if every client app loads all the records from the database, whats the point of a database? Lets just load 500 mb of data into the clients memory. 10 clients, 5 gig of network traffic..

Better off using parameterized query and only loading the data you need. See the DW1 link in my signature for more information
 
What I did was open the database and filled a new DataGrid when the form was opened. Then when I search by the criteria, I put under a button, this code:
VB.NET:
Dim dv As New DataView(ds.Tables("TableName"))
            Dim x As Integer = dv.Count

            dv.Sort = "Sender"
            dv.RowFilter = "sender = '" & Trim(SenderBox.Text) & "'"
            NameOfDataGridOnForm.DataSource = dv
It was really an error on my part because I kept trying to fill a new dataGrid with the search criteria.
Now I'm having problems filtering a date range. It sorts it but it gives me all records.
 
Last edited:
Back
Top