473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to fill a DataGridView without freezing the UI thread

The auto-generated line of code to fill a DataGridView once it is placed on
the designer surface in VS2005 is typically:

this.xyzTableAdapter.Fill(this.myDataSet.MyMember) ;

But if the query takes a long time to run, this freezes the UI (in a Windows
Forms application). What is the best practice for filling a DataGridView
without freezing the UI?
Apr 20 '07 #1
2 11995
Actually, that is filling a DataSet/DataTable, not a DataGridView; to
do this without blocking, simply don't attach the set/table to the
DataGridView (as DataSource), and populate it on a non-UI thread, then
jump back when full to populate.

Something like (untested):

void fillButton_Click(object sender, EventArgs args) { // called on UI
thread
// may need to detach DataSource here (if present) or suspend
binding (RaiseListChangedEvents?)
myDataGridView.DataSource = null;

// spawn some work in the background
ThreadPool.QueueUserWorkItem(FillData);
}

void FillData(object state) { // called on background thread
this.xyzTableAdapter.Fill(this.myDataSet.MyMember) ;
// OK; have some data; attach the DataSource (need to jump to UI
thread):

this.Invoke((MethodInvoker) delegate {
myDataGridView.DataSource = myDataSet; // or table
});
}

Apr 20 '07 #2
Your approach sounds reasonable, Marc. Unfortunately, I will not be able to
test it out on a very timely basis, so I am marking your post as "providing
the answer" now...

"Marc Gravell" wrote:
Actually, that is filling a DataSet/DataTable, not a DataGridView; to
do this without blocking, simply don't attach the set/table to the
DataGridView (as DataSource), and populate it on a non-UI thread, then
jump back when full to populate.

Something like (untested):

void fillButton_Click(object sender, EventArgs args) { // called on UI
thread
// may need to detach DataSource here (if present) or suspend
binding (RaiseListChangedEvents?)
myDataGridView.DataSource = null;

// spawn some work in the background
ThreadPool.QueueUserWorkItem(FillData);
}

void FillData(object state) { // called on background thread
this.xyzTableAdapter.Fill(this.myDataSet.MyMember) ;
// OK; have some data; attach the DataSource (need to jump to UI
thread):

this.Invoke((MethodInvoker) delegate {
myDataGridView.DataSource = myDataSet; // or table
});
}

Apr 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate...
8
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the...
2
by: Adrien Reboisson | last post by:
I'm trying to build a basic DB explorer using C# & Visual Studio 2005. I installed SQL Server 2005 Express, created a blank project, dropped a TreeView, a ListView and a DataGridView : DB objects...
4
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I...
0
by: =?Utf-8?B?SEdK?= | last post by:
I am looking for a beter way to show column total at the bottom of a DataGridView. The way I've done by setting labels at outside the bottom of the DataGridView and show the total values in the...
2
by: RodneyAnonymous | last post by:
I'm trying to add multi-thread capabilities to a program I am writing so the users aren't locked into loading a potentially large log file without being able to use the program for anything else....
0
by: lpizzle | last post by:
I am embedding a SQL editor in my company's existing program to allow our clients to easily run queries against our database. The current layout is a textbox where they type in the code and a...
6
by: akshaycjoshi | last post by:
I have one datagridview which i fill using the connected verion of ADO.NET. When the user presses the search button a seperate thread is created and in that thead i connect to the database and fill...
4
by: techno58 | last post by:
In my form i want to be able to fill all my controls with a particular record by selecting that record from my datagridview.If i select a particular record after clicking on it all my controls like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.