Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I have a dataset table that contains a column named "Frequency". The following are examples of the values contained in that column:

ID Frequency
------ ----------
1 30,90
2 30,90
3 90
4 30,90
5 90,365
6 90
7 15,180

I'm trying to come up with a filter expression that will allow me to select only those rows where column Frequency contains the value "30". The only ways that I can think of to approach this would be to use "LIKE" or "CONTAINS" operators.

The following is an attempt to use "LIKE"
dSet.Tables(0).DefaultView.RowFilter = "'30' IN (Frequency)"

This produces the error: {"Only constant expressions are allowed in the expression list for the IN operator."}

The following is my attemp at useing "CONTAINS":
dSet.Tables(0).DefaultView.RowFilter = "Frequency contains (30))"

This produces the error: {"Syntax error: Missing operand after 'contains' operator."}

I've tried several variations on the CONTAINS syntax... I'm not so sure that it is supported in filtering syntax.

Any nudge in the right direction would be greatly appreciated.
Posted
Updated 8-Jun-11 6:50am
v2

You're looking for

dSet.Tables(0).DefaultView.RowFilter = "Frequency LIKE '*30*'";


However, this will also select rows where the value is 300, 130, etc. So to get only values of 30 you'd need something like:

dSet.Tables(0).DefaultView.RowFilter = "Frequency = '30' OR Frequency LIKE '30,*' OR Frequency LIKE '*,30'";


and so on so that you get all of the possibilities.

If you're using v3.5 or later of the framework you can use LINQ to make a dataview that you can use as a datasource.

var view = from DataRow row in dSet.Tables(0).Rows
           from frequency in row.Frequency.Split(new[]{','})
           where frequency == "30"
           select row;
 
Share this answer
 
use this
dSet.Tables[0].DefaultView.RowFilter = "Frequency like '%30%')";


hope this will work for you.
 
Share this answer
 
v2
Comments
Yvan Rodrigues 8-Jun-11 13:19pm    
In case anyone is confused, * and % can be used interchangably in DataColumn expressions.
Rick Behrens 8-Jun-11 13:58pm    
Thanks Yvan, that worked perfectly. Appreciate your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900