From: Bill Schanks on
I have this code that worked fine, until I placed it in a
backgroundworker. The first time I generate the results, it works
fine, but on the second time I get the error:

<error>
DataGridView Default Error Dialog

The following exception occurred in the DataGridView:
System.IndexOutOfRangeException: Index 0 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError
(Int32 rowIndex)

To replace this default dialog please handle the DataError event.
</error>

Here is the code

Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) _
Handles btnGenerate.Click

'Ensure required parms are filled in
If String.IsNullOrEmpty(Me.cbAsOf.Text) Or _
(String.IsNullOrEmpty(Me.txtAcct.Text) And _
String.IsNullOrEmpty(Me.cbCID.Text) And _
String.IsNullOrEmpty(Me.cbMinor.Text)) Then

MessageBox.Show("Please input an 'As Of' Date to pull for, and at
least one other option" _
, g_sApp_Name, MessageBoxButtons.OK)
Exit Sub
End If

Try
'Update UI
_frmMain.ssStatus.Text = "Getting Acctount info"
Me.ssExecTime.Text = "Executing"
Me.ssRows.Text = "0 Rows"
_dtStart = Date.Now

'Get results
_sAcct = Me.txtAcct.Text
_sCID = Me.cbCID.SelectedValue
_sMinor = Me.cbMinor.SelectedValue

If String.IsNullOrEmpty(_sAcct) Then _sAcct = Nothing
If String.IsNullOrEmpty(_sCID) Then _sCID = Nothing
If String.IsNullOrEmpty(_sMinor) Then _sMinor = Nothing

'Run fetch in background
ReportWorker = New System.ComponentModel.BackgroundWorker
ReportWorker.WorkerSupportsCancellation = True
Me.tsStop.Enabled = True
Me.btnGenerate.Enabled = False
Me.btnReset.Enabled = False
ReportWorker.RunWorkerAsync()

'Placed in background worker
'Me.Spoc_RCT_AcctLookupTableAdapter.Fill
(Me.AcctLookupDataSet.spoc_RCT_AcctLookup _
' , CDate(Me.cbAsOf.SelectedValue) _
' , _sAcct _
' , _sCID _
' , _sMinor)

Catch ex As Exception
HandledExceptionManager.ShowDialog("An error occured while
attemting to " _
& "pull the requested report.", _
"Your report could not be retrieved.", _
"Evaluate the error, and re-try the command. If you need
assistance, " _
& "Click Help/About for contact info.", _
ex, _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
HandledExceptionManager.UserErrorDefaultButton.Default)

Finally
'Cursor = Cursors.Default
'_frmMain.ssStatus.Text = "Ready"
'Me.Spoc_RCT_AcctLookupBindingNavigator.Enabled = True

End Try

End Sub

Private Sub ReportWorker_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles ReportWorker.DoWork

Me.Spoc_RCT_AcctLookupTableAdapter.Fill
(Me.AcctLookupDataSet.spoc_RCT_AcctLookup _
, CDate(Me.cbAsOf.SelectedValue) _
, _sAcct _
, _sCID _
, _sMinor)

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As
Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles ReportWorker.RunWorkerCompleted ' Completed

'If ReportWorker.CancellationPending Then Exit Sub

'Buttons/Status
Me.tsStop.Enabled = False
Me.btnGenerate.Enabled = True
Me.btnReset.Enabled = True
_frmMain.ssStatus.Text = "Ready"

'Process time
_dtEnd = Date.Now
_ts = _dtEnd - _dtStart
Me.ssExecTime.Text = String.Format("{0:d2}:{1:d2}:{2:d2}",
_ts.Hours, _ts.Minutes, _ts.Seconds)
Me.ssRows.Text = Me.dvg_results.RowCount & " Rows"

'enable copy/save options
If Me.dvg_results.RowCount > 0 Then
Me.ts_copy.Enabled = True
Me.ts_xlSave.Enabled = True
Me.tsExportToXL.Enabled = True
Else
Me.ts_copy.Enabled = False
Me.ts_xlSave.Enabled = False
Me.tsExportToXL.Enabled = False
End If

End Sub
From: Bill Schanks on
I found this method works for what I need:
http://msmvps.com/blogs/williamryan/archive/2004/12/10/23367.aspx

On Oct 13, 4:04 pm, Bill Schanks <wscha...(a)gmail.com> wrote:
> I have this code that worked fine, until I placed it in a
> backgroundworker. The first time I generate the results, it works
> fine, but on the second time I get the error:
>
> <error>
> DataGridView Default Error Dialog
>
> The following exception occurred in the DataGridView:
> System.IndexOutOfRangeException: Index 0 does not have a value.
>    at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
>    at
> System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError
> (Int32 rowIndex)
>
> To replace this default dialog please handle the DataError event.
> </error>
>
> Here is the code
>
> Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) _
>            Handles btnGenerate.Click
>
>                 'Ensure required parms are filled in
>                 If String.IsNullOrEmpty(Me.cbAsOf.Text) Or _
>                   (String.IsNullOrEmpty(Me.txtAcct.Text) And _
>                   String.IsNullOrEmpty(Me.cbCID.Text) And _
>                   String.IsNullOrEmpty(Me.cbMinor.Text)) Then
>
>                         MessageBox.Show("Please input an 'As Of' Date to pull for, and at
> least one other option" _
>                            , g_sApp_Name, MessageBoxButtons.OK)
>                         Exit Sub
>                 End If
>
>                 Try
>                         'Update UI
>                         _frmMain.ssStatus.Text = "Getting Acctount info"
>                         Me.ssExecTime.Text = "Executing"
>                         Me.ssRows.Text = "0 Rows"
>                         _dtStart = Date.Now
>
>                         'Get results
>                         _sAcct = Me.txtAcct.Text
>                         _sCID = Me.cbCID.SelectedValue
>                         _sMinor = Me.cbMinor.SelectedValue
>
>                         If String.IsNullOrEmpty(_sAcct) Then _sAcct = Nothing
>                         If String.IsNullOrEmpty(_sCID) Then _sCID = Nothing
>                         If String.IsNullOrEmpty(_sMinor) Then _sMinor = Nothing
>
>                         'Run fetch in background
>                         ReportWorker = New System.ComponentModel.BackgroundWorker
>                         ReportWorker.WorkerSupportsCancellation = True
>                         Me.tsStop.Enabled = True
>                         Me.btnGenerate.Enabled = False
>                         Me.btnReset.Enabled = False
>                         ReportWorker.RunWorkerAsync()
>
>                         'Placed in background worker
>                         'Me.Spoc_RCT_AcctLookupTableAdapter.Fill
> (Me.AcctLookupDataSet.spoc_RCT_AcctLookup _
>                         '  , CDate(Me.cbAsOf.SelectedValue) _
>                         '  , _sAcct _
>                         '  , _sCID _
>                         '  , _sMinor)
>
>                 Catch ex As Exception
>                         HandledExceptionManager.ShowDialog("An error occured while
> attemting to " _
>                          & "pull the requested report.", _
>                          "Your report could not be retrieved.", _
>                          "Evaluate the error, and re-try the command. If you need
> assistance, " _
>                          & "Click Help/About for contact info.", _
>                          ex, _
>                          MessageBoxButtons.OK, _
>                          MessageBoxIcon.Error, _
>                          HandledExceptionManager.UserErrorDefaultButton.Default)
>
>                 Finally
>                         'Cursor = Cursors.Default
>                         '_frmMain.ssStatus.Text = "Ready"
>                         'Me.Spoc_RCT_AcctLookupBindingNavigator.Enabled = True
>
>                 End Try
>
>         End Sub
>
>         Private Sub ReportWorker_DoWork(ByVal sender As Object, _
>                 ByVal e As System.ComponentModel.DoWorkEventArgs) _
>                 Handles ReportWorker.DoWork
>
>                 Me.Spoc_RCT_AcctLookupTableAdapter.Fill
> (Me.AcctLookupDataSet.spoc_RCT_AcctLookup _
>                   , CDate(Me.cbAsOf.SelectedValue) _
>                   , _sAcct _
>                   , _sCID _
>                   , _sMinor)
>
>         End Sub
>
>         Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As
> Object, _
>          ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
>            Handles ReportWorker.RunWorkerCompleted      ' Completed
>
>                 'If ReportWorker.CancellationPending Then Exit Sub
>
>                 'Buttons/Status
>                 Me.tsStop.Enabled = False
>                 Me.btnGenerate.Enabled = True
>                 Me.btnReset.Enabled = True
>                 _frmMain.ssStatus.Text = "Ready"
>
>                 'Process time
>                 _dtEnd = Date.Now
>                 _ts = _dtEnd - _dtStart
>                 Me.ssExecTime.Text = String.Format("{0:d2}:{1:d2}:{2:d2}",
> _ts.Hours, _ts.Minutes, _ts.Seconds)
>                 Me.ssRows.Text = Me.dvg_results.RowCount & " Rows"
>
>                 'enable copy/save options
>                 If Me.dvg_results.RowCount > 0 Then
>                         Me.ts_copy.Enabled = True
>                         Me.ts_xlSave.Enabled = True
>                         Me.tsExportToXL.Enabled = True
>                 Else
>                         Me.ts_copy.Enabled = False
>                         Me.ts_xlSave.Enabled = False
>                         Me.tsExportToXL.Enabled = False
>                 End If
>
>         End Sub