Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am writing a C# Windows Forms application. I read some information from a file and fill a datagridview. The program opens only after the reading and filling is complete (which lasts 4-5 seconds). What I want is to load and show the interface first and later load the informations to datagridview simultaneously. (Something like add/remove-programs interface of windows) Is it even possible?
Posted

Of course it is possible, and it is the way it should be done; nobody likes a system that seems not to react for several seconds.

What you need is eliminate all long-winding operations from the Form's constructor or its Load handler, including explicit calculations as well as data bindings. You could delay them till the Shown event; and even then you should consider putting them in a separate thread (maybe a BackgroundWorker); that would make the GUI alive (so it repaints when it gets covered and uncovered by other objects, etc), however it makes updating the GUI slightly more difficult. You may want to read this[^].

:)
 
Share this answer
 
yes you have to do 3 things

1) spawn off a background thread process
see http://support.microsoft.com/kb/815804[^] for an example

2) in the background thread load your file

3) once the data is loaded you have to marshall a method back to your main thread and then bind your data to your datagridview.
This would be something like
private void BindData()
{
  If (myDataGrid.InvokeRequired)
  {
    myDataGrid.BeginInvoke(BindData)
  }

  ... code to do binding
  myDataGrid.DataSource = ...
}


I'd also disable the datagridview by default and only enable it once you have data to bind to it.

see example http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx[^]
 
Share this answer
 
It's very possible...though not always easy, depending on what all you want to do with it. I'm doing something similar with one of my forms.

I have a series of ComboBoxes that get filled in from data in a database. It takes a while for the database to return the data, so I have a PictureBox in front of the ComboBoxes with a simple Loading gif that is shown when I first make the call to fill in the boxes. When the boxes are finished loading, I simply hide the PictureBox.

So, the real way that I do it is complicated. The basics are that I use a BackgroundWorker to load the data from the database. When the BackgroundWoker is complete, it raises an event that I have hooked that tells me that I can hide the PictureBox and enable the ComboBoxes.

It's quite a bit more complicated than that including Invoke/InvokeRequired and several events, but that's the main idea.

I've considered writing up an article about what I've been doing just as an example of how you can use BackgroundWorker's to load different controls when parts of the BackgroundWorker have completed.
 
Share this answer
 
Thanks for all the answers!! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900