I am using the code below to allow a user to filter a DataTable
by searching a particular string that could be in any column or any row. The code needs to delete rows where the value doesn't exist as the DataTable
is exported after the operation.
The problem with this code is two-fold: 1) it is extremely slow for larger tables, and 2) it can only find the complete contents of a cell (i.e. if the column "Name" has a row where the value is "Andrew" the user should be able to search "drew" or "and" and get that result; right now it will return that row if they search "Andrew").
if(!String.IsNullOrEmpty(combo1Text) || !String.IsNullOrEmpty(combo2Text)
&& !String.IsNullOrEmpty(search1Text) && !search1Text.Contains("Type your search for" + comboText + "here"))
{
for (int i = tab1table.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = tab1table.Rows[i];
if (!dr.ItemArray.Contains(search1Text))
{
dr.Delete();
tab1table.AcceptChanges();
}
percentprogress++;
worker.ReportProgress(percentprogress);
}
}
What is the best way to do the filtering I want (and do so efficiently, so that it's not just looping through everything)?