2 Filtering conditions in Dataview

ahassan99

Member
Joined
Nov 24, 2007
Messages
9
Programming Experience
Beginner
Hi Guys,

In my application I have a search facility in which I am using a data reader to fetch the records according to a SQL statement. Also, in a datagrid I show the results as it is more convenient to the user. The way I am showing the data in the datagrid is that I create a dataview of the table in the DB, then I use the ROWFILTER method to filter the records according to the entry of the user.
E.G. Dataview.RowFilter = "Item_name LIKE '%" & txtsearchby.Text & "%'"Then I use the dataview as the source of the datagrid.
DataGrid1.DataSource = dv

The problem now is when I need to filter the records in the dataview according to more than one column. Say 2 columns.

What is the best way for me to do this ?!

Any help will be much appreciated !
 
put AND in ?

I'd also tidy up your filter code so it's something like

VB.NET:
Dim strSearchBy as String = cstr("%" & me.txtsearchby.text & "%")

Dataview.RowFilter = "Item_name LIKE" & strSearchBy
DataGrid1.DataSource = DV

You just adjust the filter query to include another column;

VB.NET:
Dim strSearchBy as String = cstr("%" & me.txtsearchby.text & "%")
Dim strSurname as String = me.txtSurname.text

Dataview.RowFilter = "Item_name LIKE " & strSearchBy & " AND " & strSurname
DataGrid1.DataSource = DV

So that will list only rows that are like what is in the SearchBy textbox, and also match the surname.... you can of course have AND, or use OR, or LIKE .... and if you have a Boolean field you can use = True or = False ...
 
Before setting the datatable or dataset to dataview,try to make the data to be bind to grid to a datatable.Then u can use select property of datatable to set the filtering.
U can do any number of filtering options.
VB.NET:
 For Each row As DataRow In Tempds.Tables(0).Select(filter, sort)
                    Dim row1 As DataRow
                    row1 = useraccDt.NewRow()
                    For i As Integer = 0 To useraccDt.Columns.Count - 1
                        row1.Item(i) = row(i)
                    Next
                    useraccDt.Rows.Add(row1)
                Next

is an example.
Here filter is a string.
for ex:filter="name=John and sal=5000"
 
Last edited by a moderator:
Dim strSearchBy as String = cstr("%" & me.txtsearchby.text & "%")

Thats tidy? Why do you CStr something that is already a string?

Dataview.RowFilter = "Item_name LIKE " & strSearchBy & " AND " & strSurname
I dont think you can say:

Item_nam LIKE '%orange%' AND smith


It doesnt look legal syntax to me
 
Thats tidy? Why do you CStr something that is already a string?
Urm...dunno....

I dont think you can say:

Item_nam LIKE '%orange%' AND smith

It doesnt look legal syntax to me

You are quite right. In my hastily written reply I'd forgot to add the 2nd column. It should read
VB.NET:
Dataview.RowFilter = "Item_name LIKE " & strSearchBy & " AND Surname LIKE " &  strSurname

well spotted young man.... :D
 
Back
Top