Populate a dataGridView with a large set of results from an SQL query in C # Windows Forms

I have windows forms created using dataGridView. I also have a long SQL query that I run in the BackgroundWorker thread to populate a static datatable.

private void RunQuery_DoWork(object sender, DoWorkEventArgs e) { OdbcDataAdapter adapter = new OdbcDataAdapter(longRunningSQLQuery, datasourcename); adapter.Fill(results); } private void RunQuery_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { dataGridView1.DataSource = results; } 

This works great. When I go to run the query, the user interface remains responsive as the results are generated in the background ... However, when it is time to display the results in my data file, the window freezes if it contains a large data set. If I let him sit for a while, in the end he ends. In my RunWorkerCompleted callback function, I call dataGridView1.DataSource = results; (the results are my DataSet), and this is the part that takes a lot of time.

Is there any way to pre-bind the dataGridView or bind it to the desktop?

+4
source share
4 answers

If you use Virtualmode for a datagrid, only the displayed rows are loaded. Otherwise, all rows will be loaded.

http://msdn.microsoft.com/en-us/library/2b177d6d.aspx

+4
source

It will use reflection to pull values ​​from the data source. A faster way might be to create and add lines yourself. Perhaps you can create a list of lines in the background thread, and then add them as a range in the main user interface thread - not sure.

Obviously, you are losing the benefits of data binding, but if you want to load a gigantic amount of data, this may be the only option . It turns out you can use VirtualMode, you implement the parts yourself, but retain the benefits of data binding.

+1
source

Since the GridView is a visual component, I think this rule applies that you can only call methods and properties from the foreground thread that created these controls.

Perhaps you can complete the grid step by step, adding each, for example. 100 entries per iteration, which makes control more smooth.

Microsoft SQL Server Management Studio seems to be using a GridView as well as a large descriptor size.

0
source

Source: https://habr.com/ru/post/1332511/


All Articles