I declared a delegate in one of my classes.

Private Delegate Sub AsyncDelegate(ByVal ar As IAsyncResult)

And I am using it to asynchronously fetch and load data in my grid.

VB Code:
  1. Private Sub CallBackGrid(ByVal ar As IAsyncResult)
  2.         If ar Is Nothing Then
  3.             m_waitWindow.lblMessage.Text = "Please wait.. while Loading Grid."
  4.             m_waitWindow.Show()
  5.             Dim RunDelegate As AsyncDelegate = AddressOf FillGrid
  6.             RunDelegate.BeginInvoke(ar, AddressOf CallBackGrid, RunDelegate)
  7.         ElseIf ar.IsCompleted Then
  8.             m_waitWindow.Hide()
  9.         End If
  10.     End Sub
  11.  
  12.     Private Sub FillGrid(ByVal ar As IAsyncResult)
  13.         Dim entityObject As New B_Entity
  14.         Dim whereClause, orderBy As String
  15.         Dim entitySchema As String
  16.         Dim recordCount As Long
  17.  
  18.         If txtLabel.Text.Trim <> "" Then
  19.             whereClause = "Label Like '" & txtLabel.Text & "%'"
  20.         End If
  21.         If txtParentEntity.Text.Trim <> "" Then
  22.             whereClause = "ParentEntity Like '" & txtParentEntity.Text & "'"
  23.         End If
  24.         If txtTableName.Text.Trim <> "" Then
  25.             whereClause = "TableName Like '" & txtTableName.Text & "'"
  26.         End If
  27.         If txtLevel.Text.Trim <> "" Then
  28.             whereClause = "Level = " & txtLevel.Text
  29.         End If
  30.  
  31.         orderBy = "TableName ASC"
  32.         whereClause = ""
  33.         orderBy = ""
  34.  
  35.         Try
  36.  
  37.             Dim entityData As DataTable = entityObject.GetEntities(m_Page, CType(txtRecordsPerPage.Text, Integer), "*", "CRM_UDF_GetConfig_Entities()", whereClause, orderBy, recordCount)
  38.             entitySchema = "Entity_Guid,ParentEntity_Guid,TableName,SortOrder,Level,Entity_Label,ParentEntity_Label,Active"
  39.  
  40.             spdEntities.ActiveSheet.RowCount = entityData.Rows.Count
  41.  
  42.             Dim i, j As Integer
  43.  
  44.             For i = 0 To entityData.Rows.Count - 1
  45.                 spdEntities.ActiveSheet.SetActiveCell(i, 1)
  46.                 spdEntities.ActiveSheet.Cells(i, 1).Value = entityData.Rows(0)("Entity_Guid").ToString
  47.                 spdEntities.ActiveSheet.Cells(i, 2).Value = entityData.Rows(0)("ParentEntity_Guid").ToString
  48.                 spdEntities.ActiveSheet.Cells(i, 3).Value = entityData.Rows(0)("Entity_Label").ToString
  49.                 spdEntities.ActiveSheet.Cells(i, 4).Value = entityData.Rows(0)("ParentEntity_Label").ToString
  50.                 spdEntities.ActiveSheet.Cells(i, 5).Value = entityData.Rows(0)("TableName").ToString
  51.                 spdEntities.ActiveSheet.Cells(i, 6).Value = entityData.Rows(0)("SortOrder").ToString
  52.                 spdEntities.ActiveSheet.Cells(i, 7).Value = entityData.Rows(0)("Level").ToString
  53.  
  54.                 spdEntities.ActiveSheet.Cells(i, 8).Value = entityData.Rows(0)("Active")
  55.             Next
  56.  
  57.             txtTotalRecords.Text = CType(recordCount, String)
  58.  
  59.         Catch ex As Exception
  60.             Debug.WriteLine(ex.Message)
  61.         End Try
  62.     End Sub

It gives this error message.

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Invalid row index: 0 (must be between -1 and -1).

But when I comment all the CallBackGrid code and put only one line FillGrid(nothing), it works like a charm. What is wrong with delegate I do not understand.