Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can search rows in a datatable with a searchstring?

Tags:

I want to search rows in my DataTable.

I've tried this:

 protected void imggastsuche_Click(object sender, EventArgs e)          {             string searchstring = txtgastsuche.Text;              DataTable tb = DataBaseManager.GetDataTable(mysqlconnectionstring);              DataRow[] foundRows = tb.Select("FIRSTNAME,LASTNAME,NAME,COMPANY,TIMEFROM,TIMETO,CREATOR Like '%" + searchstring + "%'");              tb = foundRows.CopyToDataTable();              this.ListView.DataSource = tb;             this.ListView.DataBind();          } 

But I have an error in my string.

What can I do if I want to search these columns?

like image 526
Tarasov Avatar asked Jan 24 '13 08:01

Tarasov


People also ask

How do you filter data in a DataTable?

Filtering DataTable varieties of ways include select(String) method, which selects the required row or column and then based on that applies the filter. Filtering can be done using Select, Where, AND, OR, NOT logical operator and on top of it applying the value also there.

How do you find the data in a DataTable?

Searching on individual columns can be performed using the columns().search() and column().search() methods. DataTables has a built in search algorithm referred to as "smart" searching and is designed to make searching the table data, easy to use for the end user.

How do I find the index of DataRow?

Hi every one. DataRow[] dr = dt1. Select("name='" + result[k2] + "' and school='" + result1[k3] + "'");.


1 Answers

You get the error because the parameter to Select is the filterExpression and you have passed all columns. Understand the filterExpression as a WHERE clause in sql. You want all columns but you want to filter by just one. You get all columns anyway since they are all part of the DataTable/DataView so you don't need to list them explicitely.

You could either use the DataTable.Select, DatView.RowFilter methods or LINQ-to-DataSet:

LINQ-To-DataSet (which i prefer):

var filtered = tb.AsEnumerable()     .Where(r => r.Field<String>("CREATOR").Contains(searchstring)); 

ADO.NET(DataTable.Select):

DataRow[] filteredRows = tb.Select("CREATOR LIKE '%" + searchstring + "%'"); 

ADO.NET(DataView.RowFilter):

 tb.DefaultView.RowFilter = "CREATOR LIKE '%" + searchstring + "%'"; 

If you want to search for this string in any column instead:

DataRow[] filteredRows = tb.Select("FIRSTNAME LIKE '%" + searchstring + "%' OR LASTNAME LIKE '%" + searchstring + "%' OR NAME LIKE '%" + searchstring + "%' OR COMPANY LIKE '%" + searchstring + "%' OR CREATOR LIKE '%" + searchstring + "%'"); 

The same with Linq:

var filtered = tb.AsEnumerable()     .Where(r => r.Field<String>("FIRSTNAME").Contains(searchstring)            ||   r.Field<String>("LASTNAME").Contains(searchstring))            ||   r.Field<String>("NAME").Contains(searchstring)            ||   r.Field<String>("COMPANY").Contains(searchstring)            ||   r.Field<String>("CREATOR").Contains(searchstring)); 
like image 148
Tim Schmelter Avatar answered Sep 17 '22 15:09

Tim Schmelter