问题
I've Trying to filter a DataView
which have multiple columns where some is numerical values. Everything works perfectly when typing in a string but once a numerical value gets entered (like checking for courier #) it filters to nothing.
How My Data Displays before filter:
How My Data Displays after filter:
My Code is as followed:
private void tbxSearchCourier_KeyUp(object sender, KeyEventArgs e)
{
string outputInfo = "";
string[] keyWords = tbxSearchCourier.Text.Split(' ');
foreach (string word in keyWords)
{
if (outputInfo.Length == 0)
{
outputInfo = "('Courier #' LIKE '%" + word + "%' OR Name LIKE '%" + word + "%' OR Branch LIKE '%" + word + "%' OR 'Contact Number' LIKE '%" + word
+ "%' OR 'Email Address' LIKE '%" + word + "%')";
}
else
{
outputInfo += " AND ('Courier #' LIKE '%" + word + "%' OR Name LIKE '%" + word + "%' OR Branch LIKE '%" + word + "%' OR 'Contact Number' LIKE '%" + word
+ "%' OR 'Email Address' LIKE '%" + word + "%')";
}
}
CourierDV.RowFilter = outputInfo;
}
I've search for a solution on the web but can't find anything that works. Why is this happening and how can I fix it?
回答1:
Consider these notes:
You've put the column name between
''
which makes it as a string literal.Use
[]
around column name if it's a complex name.To compare an
integer
column withLIKE
operator, you should first convert it tostring
becauseLIKE
is anstring
operator.Also make sure your column name is
Courier #
and it's not the caption/header text.
Example
dataView.RowFilter = "Convert([Some Column], System.String) LIKE '12%'";
More Information
To learn more about supported syntax for filter, take a look at DataColumn.Expression.
来源:https://stackoverflow.com/questions/39936409/filter-an-integer-using-like-in-bindingsoure-filter-or-dataview-rowfilter