How I can filter a Datatable? How I can filter a Datatable? asp.net asp.net

How I can filter a Datatable?


You can use DataView.

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


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


If you're using at least .NET 3.5, i would suggest to use Linq-To-DataTable instead since it's much more readable and powerful:

DataTable tblFiltered = table.AsEnumerable()          .Where(row => row.Field<String>("Nachname") == username                   &&   row.Field<String>("Ort") == location)          .OrderByDescending(row => row.Field<String>("Nachname"))          .CopyToDataTable();

Above code is just an example, actually you have many more methods available.

Remember to add using System.Linq; and for the AsEnumerable extension method a reference to the System.Data.DataSetExtensions dll (How).


use it:

.CopyToDataTable()

example:

string _sqlWhere = "Nachname = 'test'";string _sqlOrder = "Nachname DESC";DataTable _newDataTable = yurDateTable.Select(_sqlWhere, _sqlOrder).CopyToDataTable();