4 Replies - 2422 Views - Last Post: 02 May 2011 - 01:38 PM Rate Topic: -----

#1 callahan09  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 61
  • Joined: 13-April 10

DataGridView scrollbars & threads. Please help!

Posted 30 April 2011 - 06:43 PM

Hi, all.

I'm using a DataGridView control and populating it's row collection programatically. Whenever a button is clicked, it clears the DataGridView row collection and then populates it again with new rows depending on user criteria.

Sometimes only 1 or a few rows will populate the control, and that's fine, but sometimes it can be a couple of hundred rows, and when this happens, the form freezes for a number of seconds or even more than a minute as the rows are added to the collection and the form refreshes.

This is unacceptable. So I've tried a whole lot of different methods to make the form paint itself even as the rows are being populated. Problem is, none of these methods work correctly. The problem that arises 100% of the time is that the scrollbars are "frozen", unusable, for the DataGridView control whenever I try to populate the rows from a background thread instead of from the main UI thread.

I don't really understand why this is happening and I've scoured google for 2 hours now and tried so many different things invluding Invoke and BeginInvoke and a number of other things, and nothing has solved the issue.

SUMMARY: The scrollbars work perfectly fine after the rows are all finished being added if I am doing it NOT in a background thread. But that presents the problem of the UI freezing until all rows are added. So if I put the row population in the background thread, it solves the UI freezing problem EXCEPT for the scrollbars on the DataGridView control, which are indefinitely frozen or not even visible.

So to be a bit more specific, I tried this:

private void cmdSearch_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(thread));
            t.Start();
        }

void thread()
{
  resultsVolumeSearch.Rows.Clear();
  List<Volume> volumes = Volume.SearchVolumes(txtQuery.Text, 0);

            foreach (Volume v in volumes)
            {
                Image img = DownloadImage(v.image);
                resultsVolumeSearch.Rows.Add(new object[] { img, v.id, v.name, v.info, v.details, v.count });
            }
}


And I've got
CheckForIllegalCrossThreadCalls = False
set for the form.

So I really need to know what I can do to get my DataGridView rows to update without freezing my UI during the process and without messing up with scrollbars after it's done.

Any suggestions??? Thanks in advance.

This post has been edited by callahan09: 30 April 2011 - 06:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView scrollbars & threads. Please help!

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: DataGridView scrollbars & threads. Please help!

Posted 01 May 2011 - 03:44 AM

Why don't you load as much data as you need? To explain myself: when the form loads, you query data for 50 rows(let's say 50 or an acceptable number of rows that will make your UI keep going). Your query should return the data in the most convenient way to the user, so the probability that he needs to scroll down to diminish. When the user scrolls down and gets at the end of the current list, you fetch the next 50 rows from database(or the place you fetch it).
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5440
  • View blog
  • Posts: 11,672
  • Joined: 02-June 10

Re: DataGridView scrollbars & threads. Please help!

Posted 01 May 2011 - 06:10 AM

foreach (Volume v in volumes)
            {
                Image img = DownloadImage(v.image);
                resultsVolumeSearch.Rows.Add(new object[] { img, v.id, v.name, v.info, v.details, v.count });
            }



So you could have a couple hundred rows, and you make a new object and do a download for each one in the same thread as your GUI. Seems like a reason for the GUI to become unresponsive to me.

Can this be reworked? Are there really a couple hundred different images, or are their only 5 different volume images that get re-used over and over. Can those images be moved into resources within the app? Maybe you need to make your objects first with the download taking place in its own thread. Then loop through the completed collection to add it to the GUI.
Was This Post Helpful? 1
  • +
  • -

#4 callahan09  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 61
  • Joined: 13-April 10

Re: DataGridView scrollbars & threads. Please help!

Posted 02 May 2011 - 01:23 PM

View PosttlhIn`toq, on 01 May 2011 - 08:10 AM, said:

foreach (Volume v in volumes)
            {
                Image img = DownloadImage(v.image);
                resultsVolumeSearch.Rows.Add(new object[] { img, v.id, v.name, v.info, v.details, v.count });
            }



So you could have a couple hundred rows, and you make a new object and do a download for each one in the same thread as your GUI. Seems like a reason for the GUI to become unresponsive to me.

Can this be reworked? Are there really a couple hundred different images, or are their only 5 different volume images that get re-used over and over. Can those images be moved into resources within the app? Maybe you need to make your objects first with the download taking place in its own thread. Then loop through the completed collection to add it to the GUI.


Thanks for your attention. To answer one question: each volume has its own unique image. So here's what I've decided to do for now, and it's really made the program run a lot more smoothly. Rather than have each volume's image appear in its row in the DataGridView, I've decided to put a PictureBox next to the DataGridView and only download the volume's image and display it if it's row is selected in the DataGridView. So now it populates the DataGridView without images and only downloads the image one at a time as a row is selected. This definitely helped to make the UI run more efficiently.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5440
  • View blog
  • Posts: 11,672
  • Joined: 02-June 10

Re: DataGridView scrollbars & threads. Please help!

Posted 02 May 2011 - 01:38 PM

Glad it helped.

Don't forget to show anyone who helps you on any question your appreciation by hitting the green + button. It also helps other readers see which answers were the most helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1