DataViews and Data Binding—Filter operators
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| Database Articles |
| © 2003 O'Reilly Media, Inc. |
Filter operators
Like the WHERE clause, the RowFilter property allows a wide range of operators
and functions for both numeric and string data types. Consider some of the
following examples:
// Find all the rows that match // one of the three specified countries. ds.Tables["Customers"].DefaultView.RowFilter = "Country IN ('Argentina', 'Canada', 'Japan')"; // Find all the rows where a // Country isn’t specified. ds.Tables["Customers"].DefaultView.RowFilter = "Country IS NULL"; // Use alphabetic comparison to find all // the rows where the Country // starts with S or any letter after it in // the alphabet (including Switzerland, USA, UK, // Venezuela, and so on). ds.Tables["Customers"].DefaultView.RowFilter = "Country > 'S'";
After you apply a sort, you can read the DataView.Count property to determine how many rows meet the criteria and will be displayed in data bound controls.
With numeric values, you can use ranges or mathematical operators to filter rows. For example, here are some filters for the Products table:
// Find all the rows where UnitPrice is greater than 10. ds.Tables["Products"].DefaultView.RowFilter = "UnitPrice > 10"; // Find all the rows where UnitPrice is above 10 but below 15. // This is an exclusive range. ds.Tables["Products"].DefaultView.RowFilter = "UnitPrice > 10 AND UnitPrice < 15"; // Find all the rows where UnitPrice is anywhere from 10 to 15. // This is an inclusive range. ds.Tables["Products"].DefaultView.RowFilter = "UnitPrice BETWEEN 10 AND 15"; // Find all prodcuts where the total stock value is at least $1000. ds.Tables["Products"].DefaultView.RowFilter = "UnitPrice * UnitsInStock > 1000";
Table 12-1 lists the most common filter operators.
Table 12-1. Filter operators
| Operator | Description |
AND
| Combines more than one clause. Records must match all criteria to be displayed. |
OR
| Combines more than one clause. Records must match at least one of the filter expressions
to be displayed. |
NOT
| Reverses an expression. Can be used in conjunction with any other clause. |
<, >, <=, and >=
| Performs comparison of values. These comparisons can be numeric (with numeric data types) or alphabetic dictionary comparisons (with string data types). |
BETWEEN
| Specifies an inclusive range. For example, Units BETWEEN 5 AND 15 selects rows that have a value in the Units column from 5 to 15.
|
<> and =
| Performs equality testing. |
IS NULL
| Tests the column for a null value. |
IN(a,b,c)
| A short form for using an OR clause with the same field. Tests for equality between a column and the specified values (a, b, and c).
|
LIKE
| Performs pattern matching with string data types. |
+
| Adds two numeric values, or concatenates a string. |
-
| Subtracts one numeric value from another. |
*
| Multiplies two numeric values. |
/
| Divides one numeric value by another. |
%
| Finds the modulus (the remainder after one number is divided by another). |
|

