Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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+"%'"  ;
like image 212
priyanka Avatar asked Feb 26 '11 06:02

priyanka


People also ask

What is DataView RowFilter in C#?

Gets or sets the expression used to filter which rows are viewed in the DataView. public: virtual property System::String ^ RowFilter { System::String ^ get(); void set(System::String ^ value); }; C# Copy.

What is DefaultView in DataTable?

The DefaultView property returns a DataView you can use to sort, filter, and search a DataTable.

Can you use a DataView to filter rows in a DataTable?

Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data in the underlying DataTable: the content, ordering, and membership reflect changes as they occur.

What is DataView in VB net?

VB.NET ADO.NET DataView Tutorial The DataView provides different views of the data stored in a DataTable. That is we can customize the views of data from a DataTable. DataView can be used to sort, filter, and search the data in a DataTable , additionally we can add new rows and modify the content in a DataTable.


1 Answers

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+"%'"  ;
like image 112
harpo Avatar answered Nov 14 '22 23:11

harpo