Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Instantiating a datagrid fill
 

Instantiating a datagrid fill

Hi,

I have a form (Form1) with a button on it, which when pressed displays a dialog (Form2), which contains a datagrid.

My question is, how do I start the process to fill the datagrid after the dialog has finished initialising (e.g. displayed), but without any user intervention. I could use a thread, but I want to make sure the initialisation code is completed and th dailog is fully displayed before the fill commences?

Is there an event that I can use?

Using Win32 I would have used PostMessage from within the dialog init section to ensure the datagrid fill process started once the dialog initialisation code had completed.

P.S. I am aware that the datagrid fill needs to run in a secondary thread.

Regards

c# newbie

ron2464  Saturday, July 23, 2005 12:44 PM

You can use Form.Load - it fires after initialization but before the Form is displayed (it's safe to access the Form child controls in the Load event).  Given you're going to fire off a background thread, this will effectively work as if you started the work when the Form is displayed. 

Note that you cannot fill the DataGrid on the background thread as Windows Forms doesn't support control access from the non-UI thread.  You can fill a DataSet (or equivalent) on the background thread and then bind it to the DataGrid on the UI thread.  If you are using VS 2005, you can use the BackgroundWorker component to simplify this task.

Also note that if for some reason, you really need to do your work later than the Form.Load, then you can do the equivalent of PostMessage by calling this.BeginInvoke on a method (this calls PostMessage).  For example, you could call this.BeginInvoke in your Form.Load to post a message to the Form.  The VS 2005 code for this would look like this:


private void LoadData()
{
    /* Fire off background worker here */
}

private void Form2_Load(object sender, EventArgs e)
{
    this.BeginInvoke(new MethodInvoker(this.LoadData));
}

 



Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe Stegman  Saturday, July 23, 2005 4:50 PM

You can use Form.Load - it fires after initialization but before the Form is displayed (it's safe to access the Form child controls in the Load event).  Given you're going to fire off a background thread, this will effectively work as if you started the work when the Form is displayed. 

Note that you cannot fill the DataGrid on the background thread as Windows Forms doesn't support control access from the non-UI thread.  You can fill a DataSet (or equivalent) on the background thread and then bind it to the DataGrid on the UI thread.  If you are using VS 2005, you can use the BackgroundWorker component to simplify this task.

Also note that if for some reason, you really need to do your work later than the Form.Load, then you can do the equivalent of PostMessage by calling this.BeginInvoke on a method (this calls PostMessage).  For example, you could call this.BeginInvoke in your Form.Load to post a message to the Form.  The VS 2005 code for this would look like this:


private void LoadData()
{
    /* Fire off background worker here */
}

private void Form2_Load(object sender, EventArgs e)
{
    this.BeginInvoke(new MethodInvoker(this.LoadData));
}

 



Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe Stegman  Saturday, July 23, 2005 4:50 PM
Joe, excellent! Thank you very muchBig Smile

Can you recommend any good C#/.NET books where I can obtain this type of information, or anywhere else for that matter?

Regards

c# newbie
ron2464  Saturday, July 23, 2005 8:08 PM

There are a lot of books out there and I have not read or even skimmed them all.  Of the ones I have looked at, I found the following to be one of the better Windows Forms books:
http://www.amazon.com/exec/obidos/ASIN/0321116208/amzna9-1-20/ref=nosim/002-0263829-5945602?dev-t=D26XECQVNV6NDQ%26camp=2025%26link_code=xm2

And the following to be one of the better general .NET Framework books:
http://www.amazon.com/exec/obidos/tg/detail/-/0735614229/qid=/sr=/ref=cm_lm_asin/002-0263829-5945602?v=glance

And from a data/business object perspective:
http://www.amazon.com/exec/obidos/ASIN/1590593448/amzna9-1-20/ref=nosim/002-0263829-5945602?dev-t=D26XECQVNV6NDQ%26camp=2025%26link_code=xm2

There are lots of C# books however, I haven't read many and hesitate to make a recommendation.

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


Joe Stegman  Saturday, July 23, 2005 9:29 PM

You can use google to search for other answers

Custom Search

More Threads

• VS Constantly Crashes in Dataset Designer and Dataset Configuration Wizard
• How to duplicate records within the same table?
• whenever user INSERTS a new record,current date must be displayed in the DATETIME picker contro
• Combobox binding and adding new column
• VS.Net IDE - All Object Data Sources Gone
• databinding to custom control overriding Text property
• Data Bound Controls not Updating underlying database
• datagrid events not firing
• Binding to a Business Logic Layer
• bindingsource.addnew and .haschanges