Exporting thousand lines from datagridview to excel using vb.net - vb.net

Hi I'm currently having a datagridview with thousand (about 3000+) rows of record and 9 columns. I was using this method I found to export the datagridview to excel. There was no problem exporting hundred of rows but when it comes to thousand of rows, it hangs and it wouldn't respond to anything.
What problem could it be? and if there's any other way that is faster/better than this?
Thank you!

The code is being executed on the UI thread so of course your application would freeze while it's executing. If you want the UI to remain responsive then you'd have to execute the code on a secondary thread. The issue there is that the data is coming from a control, so that part at least must be executed on the UI thread.
I would suggest that one possibility is to use a BackgroundWorker and do the work in the DoWork event handler. You can set up a loop that calls ReportProgress, which raises the ProgressChanged event on the UI thread and allows you to get the data in pages, then write it out to your spreadsheet on the background thread. I'll follow up with an example.

Related

How do I show a “Loading . . . please wait” with gif image message in vb.net for a long loading form?

vb.net
I have a form(called Form1) that is very slow because it contain datagridview how load data from sql server.As a result the form takes a long time to loaded.
So I create a form_Wait and in the load event of Form1 I put
Form_wait.showdialog()
Load_datagridveiw1("Select * from table1")
Form_wait.close()
My problem is the Form_Wait will be appear but the gift inside it (rectangular progress bar) is not showen ?
I used VS 2015
You cannot meaningfully show and animate GIF in a same thread as your UI sits. You would have to use a BackGroundWorker, which is basically a separate thread. Then the animation will work smoothly and the UI thread will run fine too. There are plenty of BackGroundWorker tutorials on the web.
But a huge warning horn rings reading your words. If it is such a huge amount of data causing the slow loading, I would strongly suggest to reconsider how do you fetch the data. Introducing meaningful filters and pagination are basic ways to tackle this.

VB program free freezes but still responds?

I have a winforms program written in visual Studio, coded in visual basic.
The main part of the program is a while loop that sends out serial commands, writes data to sql, does basic arithmetic, and updates controls on my form. I have this while loop in a background workers do work sub so that the controls can be updated from the background worker sub, while it is still running. After awhile of running, my form will freeze up, while halfway through updating several text boxes. However, the background worker continues to run, the sql data still gets written, and most curiously clicking a button on the form does what it is supposed to, even though we can't see that the button was pressed.
I can't think of anything that would cause behavior like this, where visually the form is frozen, but all the controls still respond.
It freezes because the UI thread is busy with the loop. It seems that you didn't implement the background worker properly and it is still attached to the UI thread.
You can add DoEvents somewhere inside your loop, which gives the UI thread some time to handle UI events. But this is just a Band-Aid fix because I don't know your code. The ideal way is to fix your background worker and its relationship with the UI thread.

.NET - Cross-Thread Call on DataGridView

I am new to multi-threading and I stumbled across an error which I was hoping someone could explain to me why was happening.
My initial program code did not have multi-threading and I was populating some charts with data from a datagridview. I was getting values like this:
Val = dgv.Rows(counter).Cells(columnName).Value
Since I was populating four charts using the same grid, I wanted to put this chart population process on multiple threads. I got an error when trying to update GUI elements (such as labels and a progress bar) and stumbled across this thread:
.NET Controls: Why aren't all calls thread-safe?
So my fix was to have the main thread update the GUI elements, and the backgroundworkers just report progress to the main thread.
However, I still getting a cross-thread error when trying to read the value using the function above. I'd like to know why this is the case.
I solved my problem using Invoke (which I believe is the correct way of doing it), ie. something like this:
dgv.Invoke(New ReadDGVCallBack(AddressOf ReadDGV), counter, "Name")
where "Name" is the column name.
However, I still am curious to know why I can't read a value from a datagridview from another thread. Since I am not modifying it, shouldn't it be fine if data is just being read? Could someone shed some light on this?
Thanks for all the help

An effective way to display multiple labels

I am looking for an effective, quick way to show multiple labels in a quick fashion. What's a good way to do this other than replacing all the .text properties of the labels one after another? It is my understanding that whenever you update a .text property the UI has to be repainted which will add to latency if you have 50+ controls to update.
I know threading is an option but when I tried this I didn't see much of a difference as I wasn't able to load 2 labels at once, I still had to wait for the UI thread before the labels would update. What other ways are there to effectively load 50+ labels quickly? The way it is now takes quite awhile(3-4s) and I feel this could be lowered. The information is being taken from a backend system so I don't have the option for datasets/etc.
You have to separate the code that retrieves the data from the database (which you do using a background thread) and the code that updates the UI (which should happen as quickly as possible because you don't want to block the UI for too long).
My suggestion would be to use the BackgroundWorker component to do retrieve the data for all 50 labels. When the BackgroundWorker raises the RunWorkerCompleted event you call the forms' SuspendLayout function, update all the values of the labels and then you call ResumeLayout. SuspendLayout stops a control from redrawing until you call ResumeLayout.
More info: BackgroundWorker, SuspendLayout, ResumeLayout

How to call multiple subs with a BackgroundWorker

I have not yet used BackgroundWorker but believe I need to use these in my code.
My app does a lot of database work, running many SQL queries in sequence. My problem appears typical, that the main form becomes unresponsive.
I want to be able to display progress using a progressbar and a toolstripstatuslabel. I am doing this already, but without the BackgroundWorker.
My code is - unsurprisingly - divided into a several subs, which are called in sequence by the main form.
All the examples I have seen include just simple BackgroundWorker DoWork events. What is the correct way to deal with calling other code? Just call the modules as usual in the DoWork event? I do understand these must contain no user interface code.
Am using VB.Net 2010
Thanks!