问题
I'm developing an application on the datagridview filtering. I'm using RowFilter property of the dataview for the filtering data. My database table contains int & varchar data type fields. And I want to use "LIKE" query in the RowFilter Property for filtering the dataview but the "LIKE" is used only for the string data type and not for int data type. So I want to convert the int
datatype field to the varchar
datatype, but I don't want to alter my table structure. I just want the datatype to be changed temporary for my filtering condition only.
Can anybody help me in resolving this problem?
string colname="ProductID";
string condition="111";
DataView dv = new DataView();
dv.Table = ds.Tables[0] ;
dv.RowFilter ="CAST ("+colname+" AS TEXT) LIKE '"+ condition+"%'" ;
回答1:
The RowFilter is being carried out by .NET, not SQL Server. It supports a limited set of expressions which are described in the Framework documentation.
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
You should be able to use LIKE as well as CONVERT, but unlike SQL server, it wants a .NET type.
dv.RowFilter ="CONVERT("+colname+", System.String) LIKE '"+ condition+"%'" ;
回答2:
You can cast the integer...
SELECT * FROM table WHERE CAST(field AS TEXT) LIKE '%123%'
回答3:
you can use either
Convert(varchar(20),value)
or
cast(value is varchar)
来源:https://stackoverflow.com/questions/5125373/set-dataview-or-datatable-defaultview-rowfilter-for-a-custom-type