Đối tượng DataView trong ASP.NET
DataView có thể chứa tất cả các đối tượng DataRow, hoặc chỉ một số hàng đã được lọc.
Ví dụ về lọc dữ liệu với DataView
Lọc chuỗi dữ liệu:
dataView.RowFilter = "Name = 'John'"; // string value
dataView.RowFilter="Name='John ''A'''"; //string with single quotes "John 'A'"
dataView.RowFilter = String.Format("Name = '{0}'", "John 'A'".Replace("'", "''"));
Lọc dữ liệu số:
dataView.RowFilter = "Year = 2008" // integer value
dataView.RowFilter = "Price = 1199.9" // float value
dataView.RowFilter=
String.Format(CultureInfo.InvariantCulture.NumberFormat,
"Price = {0}", 1199.9f);
Lọc theo ngày tháng:
dataView.RowFilter = "Date = #12/31/2008#" // date value (time is 00:00:00)
dataView.RowFilter = "Date = #2008-12-31#" // also this format is supported
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" //date and time value
dataView.RowFilter=
String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"Date = #{0}#", new DateTime(2008, 12, 31, 16, 44, 58));
Làm việc với toán tử IN
dataView.RowFilter = "Id IN (1, 2, 3)" // integer values
dataView.RowFilter = "Price IN (1.0, 9.9, 11.5)" // float values
dataView.RowFilter = "Name IN ('John', 'Jim', 'Tom')"// string values
dataView.RowFilter = "Date IN (#12/31/2008#, #1/1/2009#)" // date time values
dataView.RowFilter = "Id NOT IN (1, 2, 3)" // values not from the list
Làm việc với toán tử LIKE
dataView.RowFilter = "Name LIKE 'j*'"; // values that start with 'j'
dataView.RowFilter = "Name LIKE '%jo%'";// values that contain 'jo'
dataView.RowFilter = "Name NOT LIKE 'j*'"; //values that don't start with 'j'
dataView.RowFilter = "Name LIKE '[*]*'";// values that starts with '*'
dataView.RowFilter = "Name LIKE '[[]*'";// values that starts with '['
Ví dụ về việc dùng toán tử like
public static string EscapeLikeValue(string valueWithoutWildcards)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < valueWithoutWildcards.Length; i++)
{
char c = valueWithoutWildcards[i];
if (c == '*' || c == '%' || c == '[' || c == ']')
sb.Append("[").Append(c).Append("]");
else if (c == '\'')
sb.Append("''");
else
sb.Append(c);
}
return sb.ToString();
}
» Tin mới nhất:
» Các tin khác: