Aysnc Update of Datagrid = Empty?

  • Thread starter Thread starter Lucas Tam
  • Start date Start date
L

Lucas Tam

Hello,

I am trying to update a datagrid via an async call, but the datagrid
shows up empty. I've checked the datatable - there are indeed rows in
it...


Here is my code:


Private Sub LoadGroup()

_del = New TCP.ChangeGroupDelegate(AddressOf _oTCP.ChangeGroup)

'invoke the method asynchronously so we don't have to wait
_IA = _del.BeginInvoke(_Group, _frmGroupList, New AsyncCallback
(AddressOf BindDataGrid), _IA)

End Sub

Private Sub BindDataGrid(ByVal ar As IAsyncResult)
If ar.IsCompleted Then
'this is our callback to let us know the method is finished
'here we could use the connection
_dt = _del.EndInvoke(_frmGroupList, _IA)
'_frmGroupList.dgArticles.Invoke
(_frmGroupList.dgArticles.DataSource, New Object() {0, _dt})

_frmGroupList.dgArticles.DataSource = _dt
End If

End Sub


The program stops executing at this line but does not crash:
_frmGroupList.dgArticles.DataSource = _dt

DataGrid is empty...

Any help would be appreciated. Thanks!
 
Hello,

I am trying to update a datagrid via an async call, but the datagrid
shows up empty. I've checked the datatable - there are indeed rows in
it...


Here is my code:


Private Sub LoadGroup()

_del = New TCP.ChangeGroupDelegate(AddressOf _oTCP.ChangeGroup)

'invoke the method asynchronously so we don't have to wait
_IA = _del.BeginInvoke(_Group, _frmGroupList, New AsyncCallback
(AddressOf BindDataGrid), _IA)

End Sub

Private Sub BindDataGrid(ByVal ar As IAsyncResult)
If ar.IsCompleted Then
'this is our callback to let us know the method is finished
'here we could use the connection
_dt = _del.EndInvoke(_frmGroupList, _IA)
'_frmGroupList.dgArticles.Invoke
(_frmGroupList.dgArticles.DataSource, New Object() {0, _dt})

_frmGroupList.dgArticles.DataSource = _dt
End If

End Sub


The program stops executing at this line but does not crash:
_frmGroupList.dgArticles.DataSource = _dt

DataGrid is empty...

Any help would be appreciated. Thanks!

You can't access form control properties on a background thread... You
need to do something like:

If ar.IsCompleted Then
_dt = _del.EndInvoke(....)

If _frmGroupList.InvokeRequired Then
Dim mi As New MethodInvoker(AddressOf Me.AssignDataSet)
_frmGroupList.Invoke(mi, Nothing)
Else
Me.AssignDataSet()
End If
End If

private sub AssignDataSet()
_frmGroupList.dgArticles.DataSource = _dt
End Sub

The callback will execute on a thread pool thread, so you must marshal
all calls to the form and it's controls back to the GUI thread. Heres,
an msdn article on the subject - the code is in C#, but the points are
relavent to VB.NET as well...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
"Zaphod grinned two manic grins, sauntered over to the bar
and bought most of it."

- Zaphod in paradise.
 
The callback will execute on a thread pool thread, so you must marshal
all calls to the form and it's controls back to the GUI thread. Heres,
an msdn article on the subject - the code is in C#, but the points are
relavent to VB.NET as well...

Thanks alot... that worked great : )
 
Back
Top