MSFanboy
asked on
Search 100 strings efficiently in a datatable or datagridview with 30000 rows
Hello,
I am looking for 100 strings in a datagridview with 30000 rows. Iterating the datagridview 30000 times in an outer for loop and iterating another inner for loop 100 times and check if the searched string is found seems not efficiently because it means a iterating a loop 30000 x 100 = 3.000.000 times WoW...
What I want is if I found the string in a row of the datagridview is to set the whole row`s backcolor red and the first column which has a checkbox to true.
All 30000 rows are in the MyExtendedReader which is populated with the database data.
How can I search the DataGridView/DataTable fast?
I am looking for 100 strings in a datagridview with 30000 rows. Iterating the datagridview 30000 times in an outer for loop and iterating another inner for loop 100 times and check if the searched string is found seems not efficiently because it means a iterating a loop 30000 x 100 = 3.000.000 times WoW...
What I want is if I found the string in a row of the datagridview is to set the whole row`s backcolor red and the first column which has a checkbox to true.
All 30000 rows are in the MyExtendedReader which is populated with the database data.
How can I search the DataGridView/DataTable fast?
this is how my DataGridView is populated:
DataView MyDataView = new DataView(MyDataTable);
MyDataView.Table.Load(MyExtendedReader)
MyDataGridView.DataSource = MyDataView;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@wpcortes
I found no RowDataBound event in DataGridView for .NET ? I do not use asp.net ...
I found no RowDataBound event in DataGridView for .NET ? I do not use asp.net ...
ASKER
@wpcortes
this is the pendant in .net for RowDataBound event:
Event RowsAdded
this is the pendant in .net for RowDataBound event:
Event RowsAdded
ASKER
The idea with running the compare in the events is nice could have be mine ;-) thx a bunch!
You need to intercept an event that will give you the displayable rows and only search those for the 100 strings.
Optimizations
If there can be only 1 occurance of a string in the 100 strings then eliminate it from the 100 each time it's found
If you find a String in a row break off the search
Otherwise you will need to rethink your Data by detecting if some of the 100 strings occurs in a row as it is added to the database. Add new fields 'StringFound' or 'RowHasString' then use that to hilight it.
The 30000 x 100 problem is a n2 Algorithm problem, worst case you will have to do 3000000 compares