Асинхронная загрузка клиента служб данных DataGridView
У меня есть служба веб-API Odata, над которой я работаю. Контроллер поддерживает асинхронность, но я не могу найти хороших примеров того, как загрузить асинхронный DataGridView при извлечении данных из службы OData. Я нашел эту ссылку, в которой есть кое-что, но я не знаю, как закончить остальное, потому что в настоящее время мне нужно преобразовать DataServiceQuery в список, или DataSource не удается. http://msdn.microsoft.com/en-us/library/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, поэтому, если у вас есть примеры того или иного, я весь в ушах...
1 ответ
Несмотря на то, что это не является заполнением асинхронной таблицы строка за строкой, она заполняет весь асинхронный источник данных. Вот код, который я использовал.
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