I want to search rows in my DataTable.
I\'ve tried this:
protected void imggastsuche_Click(object sender, EventArgs e)
{
I just made a extension method to the DataTable class for this. It returns a new Datatable containing only the rows you want.
public static DataTable SearchInAllColums(this DataTable table, string keyword, StringComparison comparison)
{
if(keyword.Equals(""))
{
return table;
}
DataRow[] filteredRows = table.Rows
.Cast()
.Where(r => r.ItemArray.Any(
c => c.ToString().IndexOf(keyword, comparison) >= 0))
.ToArray();
if (filteredRows.Length == 0)
{
DataTable dtTemp = table.Clone();
dtTemp.Clear();
return dtTemp ;
}
else
{
return filteredRows.CopyToDataTable();
}
}
Usage:
DataTable dataTable = getData();
dataTable.SearchInAllColums(Keyword, StringComparison.OrdinalIgnoreCase);