Domanda

Ho un servizio di Odata di API Web che sto lavorando.Il controller supporta Async ma non riesco a trovare buoni esempi su come caricare un DataGridView Async mentre tirando i dati dal servizio Odata.Ho trovato questo link che mi ha fatto un po 'ciò che lì, ma non so come finire il resto perché attualmente devo convertire il setServiceQuery in un elenco o il DataSource fallisce. http://msdn.microsoft.com/en-Stati Uniti / Libreria / DD756367 (V= vs.110) .aspx
Il mio codice è qualcosa di simile.

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
.

Posso leggere / codice c # o vb, quindi se hai esempi di entrambi sono orecchie ...

È stato utile?

Soluzione

Mentre questo non è veramente popolato di una griglia Async riga a righe, è popolando l'intero DATASource ASYNC.Ecco il codice che ho usato.

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
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top