• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Search 100 strings efficiently in a datatable or datagridview with 30000 rows


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);
MyDataGridView.DataSource = MyDataView;

Open in new window

  • 3
1 Solution
Only a certain number of rows are visible at any one time -

You need to intercept an event that will give you the displayable rows and only search those for the 100 strings.

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

Walter RitzelSenior Software EngineerCommented:

my suggestion would be the following: independently from your will, the gridview will run over your 30000 rows anyways, but at least it will run over each according to your paging size.
My suggestion would be execute the search over each row during the rowdatabound event. And on the same event you can highlight the rows.

MSFanboyAuthor Commented:
I found no RowDataBound event in DataGridView for .NET ?  I do not use asp.net ...
MSFanboyAuthor Commented:
this is the pendant in .net for RowDataBound event:
Event RowsAdded
MSFanboyAuthor Commented:
The idea with running the compare in the events is nice could have be mine ;-) thx a bunch!

Featured Post

Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now