数据服务客户端异步加载DataGridView
题
我有一个我正在处理的Web API ODATA服务。控制器支持异步,但似乎无法在从ODATA服务中拉动数据时如何加载DataGridView异步的任何很好的示例。我确实找到了这个链接,其中有一些那里,但我不知道如何完成其余的东西,因为我目前必须将DataServiceQuery转换为列表或数据源失败。 http://msdn.microsoft.com/zh-US / LIBLUCE / DD756367(v= vs.110).aspx
我的代码是这样的。
Private Sub getDataButton_Click(sender As Object, e As EventArgs) Handles getDataButton.Click
' Define the delegate to callback into the process
Dim callback As AsyncCallback = AddressOf OnLogsQueryComplete
' Define the query to execute asynchronously that returns
' all customers with their respective orders.
Dim query As DataServiceQuery(Of LogServiceReference.Log) = (From log In context.Logs
Select log)
' Begin query execution, supplying a method to handle the response
' and the original query object to maintain state in the callback.
DataGridView1.DataSource = query.BeginExecute(callback, query)
End Sub
Private Function OnLogsQueryComplete(ByVal result As IAsyncResult) As List(Of LogServiceReference.Log)
' Get the original query from the result.
Dim query As DataServiceQuery(Of LogServiceReference.Log) = _
CType(result.AsyncState, DataServiceQuery(Of LogServiceReference.Log))
Return query.EndExecute(result).ToList()
End Function
.
我可以读取/代码c#或vb,因此如果您有任何耳朵的例子...
解决方案
虽然这不是真正填充网格行逐行的异步,它正在填充整个DataSource异步。这是我使用的代码。
Private gridRows As List(Of LogServiceReference.Log) = New List(Of LogServiceReference.Log)()
Private Delegate Sub UpdateUI()
Private oUpdateUI As UpdateUI
Private Sub getDataButton_Click(sender As Object, e As EventArgs) Handles getDataButton.Click
Try
gridRows.Clear()
DataGridView1.DataSource = Nothing
oUpdateUI = New UpdateUI(AddressOf DoUpdateUI)
' Define the delegate to callback into the process
Dim callback As AsyncCallback = AddressOf OnLogsQueryComplete
' Define the query to execute asynchronously that returns
' all customers with their respective orders.
Dim query As DataServiceQuery(Of LogServiceReference.Log) = (From log In context.Logs
Select log)
' Begin query execution, supplying a method to handle the response
' and the original query object to maintain state in the callback.
query.BeginExecute(callback, query)
Catch ex As Exception
End Try
End Sub
Private Sub DoUpdateUI()
DataGridView1.DataSource = gridRows
End Sub
Private Sub OnLogsQueryComplete(ByVal result As IAsyncResult)
' Get the original query from the result.
Dim query As DataServiceQuery(Of LogServiceReference.Log) = _
CType(result.AsyncState, DataServiceQuery(Of LogServiceReference.Log))
' Complete the query execution.
For Each log As LogServiceReference.Log In query.EndExecute(result)
gridRows.Add(log)
Next
Invoke(oUpdateUI)
End Sub
. 不隶属于 StackOverflow