hi I want to search rows in my DataTable for this I try 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 a error in my string . what can I do if I want search this columns?
If some one else needs return specifically a DataTable you can use the code below:
I just made a extension method to the DataTable class for this. It returns a new Datatable containing only the rows you want.
Usage:
you could build the query you are going to use in the select.
this equivalent to
You get the error because the parameter to
Select
is the filterExpression and you have passed all columns. Understand the filterExpression as aWHERE
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 theDataTable
/DataView
so you don't need to list them explicitely.You could either use the
DataTable.Select
,DatView.RowFilter
methods orLINQ-to-DataSet
:LINQ-To-DataSet (which i prefer):
ADO.NET(
DataTable.Select
):ADO.NET(
DataView.RowFilter
):If you want to search for this
string
in any column instead:The same with Linq: