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.
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
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...
 

jberger

Member
Joined
May 10, 2007
Messages
11
Location
Illinois
Programming Experience
Beginner
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.
 

jberger

Member
Joined
May 10, 2007
Messages
11
Location
Illinois
Programming Experience
Beginner
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:

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
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:

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
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
 

jberger

Member
Joined
May 10, 2007
Messages
11
Location
Illinois
Programming Experience
Beginner
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:
Top Bottom