Domanda

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+"%'"  ;
È stato utile?

Soluzione

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+"%'"  ;

Altri suggerimenti

You can cast the integer...

  SELECT * FROM table WHERE CAST(field AS TEXT) LIKE '%123%'

you can use either

Convert(varchar(20),value)

or

cast(value is varchar)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top