priyanka
priyanka

Reputation: 553

Set DataView (or DataTable.DefaultView) RowFilter for a custom type

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

Upvotes: 6

Views: 10732

Answers (3)

harpo
harpo

Reputation: 43228

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

Upvotes: 14

Deepak
Deepak

Reputation: 7947

you can use either

Convert(varchar(20),value)

or

cast(value is varchar)

Upvotes: -1

Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

You can cast the integer...

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

Upvotes: 0

Related Questions