How I can filter a Datatable?

Tarasov picture Tarasov · Oct 22, 2012 · Viewed 292.8k times · Source

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; 
        }

Answer

Kadir picture Kadir · Oct 22, 2012

You can use DataView.

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


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