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

Posted on 2009-05-01
Last Modified: 2012-05-06

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

Question by:MSFanboy
    LVL 12

    Expert Comment

    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

    LVL 15

    Accepted Solution


    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.


    Author Comment

    I found no RowDataBound event in DataGridView for .NET ?  I do not use ...

    Author Comment

    this is the pendant in .net for RowDataBound event:
    Event RowsAdded

    Author Closing Comment

    The idea with running the compare in the events is nice could have be mine ;-) thx a bunch!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now