Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can filter a Datatable?

I use a DataTable with Information about Users and I want search a user or a list of users in this DataTable. I try it butit don't work :(

Here is my c# code:

 public DataTable GetEntriesBySearch(string username,string location,DataTable table)         {             list = null;             list = table;              string expression;             string sortOrder;              expression = "Nachname = 'test'";             sortOrder = "nachname DESC";              DataRow[] rows =  list.Select(expression, sortOrder);              list = null; // for testing             list = new DataTable(); // for testing              foreach (DataRow row in rows)             {                 list.ImportRow(row);             }              return list;          } 
like image 364
Tarasov Avatar asked Oct 22 '12 13:10

Tarasov


People also ask

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

After a DataView has been created from a DataTable or LINQ to DataSet query, you can use the RowFilter property to specify subsets of rows based on their column values. The string-based and expression-based filters are mutually exclusive.

How do you use select box outside to filter Datatables?

First solution, once you click on drop down search box will filled with what you selected and populate the result but you can always see search box with selected value. var selectedValue=$("#month-select"). val(); table. columns(6).search( selectedValue ).

What is the best approach to filter data from a DataTable based on condition?

Using select method is the best approach to filter data from a data table based on a condition. Explanation: The 'SQL select statement' is used for returning the result of set of records from the given one or more tables of a provided database.


1 Answers

You can use DataView.

DataView dv = new DataView(yourDatatable); dv.RowFilter = "query"; // query example = "id = 10" 


http://www.csharp-examples.net/dataview-rowfilter/

like image 158
Kadir Avatar answered Sep 19 '22 14:09

Kadir