Microsoft’s Knowledgebase is full of handy information, like this: How to populate DataGrid on background thread with data binding by using Visual Basic 2005 or Visual Basic .NET
Which is handy for doing background loading/refreshing of DataBound information.
At work, I’m using a slightly different way of databinding though. I’ve got DataSources, and TableAdaptors generated by Visual Studio based on Stored Procedures in my SQL Database. This means it’s really (really) simple to do databinding - it’s fairly much a matter of drag and drop. When you want to go ahead and actually load some data - you use a 1-line function:
Me.Get_Q_Details_V111TableAdapter.Fill(Me.DsQueue.Get_Q_Details_V111, Me.QueueID)
The only issue is that your entire application locks, whilst this happens. Not fun if you need to execute 3-4 of these and update them every minute or so.
So, modifying the KB Article, I went with something like this:
Dim CallBindQueue As New MethodInvoker(AddressOf BindQueue) Dim Thread_BindQueue As Thread Dim ThreadStart_BindQueue As New ThreadStart(AddressOf RefreshQueueNow_BG)
Private Sub RefreshQueueNow_BG() Me.Get_Q_Details_V111TableAdapter.Fill(Me.DsQueue.Get_Q_Details_V111, Me.QueueID) Me.BeginInvoke(Me.CallBindQueue) End Sub
Private Sub BindQueue() Me.DataGridView1.DataSource = Me.DsQueue.Get_Q_Details_V111 End Sub
Public Sub RefreshQueueNow() Thread_BindQueue = New Thread(ThreadStart_BindQueue) Thread_BindQueue.IsBackground = True Thread_BindQueue.Name = “Queue-UpdateThread” Thread_BindQueue.Start() End Sub
To kick-off the process, you just call RefreshQueueNow, and it spawns a background thread, which does the actual fetching of data.
Because this is executing in a different thread, the standard Fill command doesn’t actually do the databinding any more - so The background thread invokes BindQueue, which sets the DataSource manually - ho-hum.
In the end - this works fine. The first time. If you ever issue another Fill command, and then for any reason need to re-draw any of the cell-contents, you’ll run into issues.
So, to fix it - you need to set the DataSource of your DataGrid to a shallow copy of the actual data. There might be a ‘neater’ way to fix this, but if there is - I don’t know about it.
So now BindQueue becomes this:
Private Sub BindQueue() Me.DataGridView1.DataSource = Me.DsQueue.Get_Q_Details_V111.Copy End Sub
If you’re only ever issuing this command once per instance, you shouldn’t need to worry.
I hope this is helpful to someone else.
Key Words: TableAdaptor SQLTableAdaptor

3 Comments
Well I’m much relieved by this post Will. It answers one of those perplexing questions that has bothered me and….
WTF is he talking about???
Yeah OK, I’ll shut up and go away now!!!
March 30th, 2007 at 11:46 pm. Permalink.
Tolkien said it best: “Do not meddle in the affairs of wizards, for they are subtle and quick to anger.”
Do you want your email, nor not?
March 31st, 2007 at 12:36 am. Permalink.
Don’t touch you touch my ‘precious’!!!
Doesn’t sound too subtle to me. Wizards indeed…huh!! LOL
March 31st, 2007 at 7:54 am. Permalink.