2017-10-06 79 views
-1

我試圖使用vb.net來封閉程序。在將excel文件「Stock.xlsx」導入到datagridview時使用Backgroundworkder的程序。但是,當我點擊開始按鈕時,會出現錯誤「索引超出了數組的範圍」。請有人幫助我們。backgroundworder錯誤 - 索引超出了數組的邊界vb.net

請參見下面的程序和快照錯誤:

公共類Form1中

Dim strRow As String()    'String array to read all fields in a row 
Dim dblAmount As Double    'Variable for total amount 
Dim blnReported As Boolean = True 'Flag to check progress report completed or not 

'Create TextFieldParser class to parse the stock.csv file in the current location 
Dim txtFile As FileIO.TextFieldParser 



Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    'Allow background operation to be cancelled 
    bgWorker.WorkerSupportsCancellation = True 

    'Allow background operation to report progress 
    bgWorker.WorkerReportsProgress = True 

    'Disable Stop Button. It is enabled only after start 
    'button is pressed 
    btnStop.Enabled = False 

    'set status message 
    lblStatus.Text = "Press Start to import data from csv." 

    'Add columns to the grid 
    dgvCSVData.Columns.Add("colNo", "No") 
    dgvCSVData.Columns("colNo").Width = 30 
    dgvCSVData.Columns.Add("colDate", "Date") 
    dgvCSVData.Columns("colDate").Width = 60 
    dgvCSVData.Columns.Add("colItem", "Item") 
    dgvCSVData.Columns("colItem").Width = 120 
    dgvCSVData.Columns.Add("colQty", "Quantity") 
    dgvCSVData.Columns("colQty").Width = 50 
    dgvCSVData.Columns.Add("colUnit", "Unit") 
    dgvCSVData.Columns("colUnit").Width = 30 
    dgvCSVData.Columns.Add("colAmt", "Amt") 
    dgvCSVData.Columns("colAmt").Width = 60 


End Sub 
Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click 

    'Disable Start button and Enable Stop 
    btnStart.Enabled = False 
    btnStop.Enabled = True 

    'set status message 
    lblStatus.Text = "Importing data from CSV file." 

    'Start time-consuming operation in background 
    Call bgWorker.RunWorkerAsync() 

End Sub 

Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click 
    'Enable Start button and disable Stop 
    btnStart.Enabled = True 
    btnStop.Enabled = False 

    'Stop background operation 
    bgWorker.CancelAsync() 

    'set status message 
    lblStatus.Text = "Import cancelled. Press Start again." 
End Sub 


Private Sub bgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork 
    Dim intCount As Int16 = 0 

    txtFile = New FileIO.TextFieldParser("stock.xlsx") 

    'Specify structure of the file 
    txtFile.TextFieldType = FileIO.FieldType.Delimited 
    txtFile.SetDelimiters(",") 

    'Skip header row 
    txtFile.ReadFields() 

    'Start reading data from file 
    While Not txtFile.EndOfData 

     If bgWorker.CancellationPending Then 
      e.Cancel = True 
      Exit Sub 

     Else 

      'Wait for Progress Report to finish 
      While Not blnReported 
       Application.DoEvents() 
      End While 

      'Read all field in a row into a string array 
      strRow = txtFile.ReadFields() 

      'Do some calculations and assign value to data grid 
      dblAmount = CDbl(strRow(3)) * CDbl(strRow(4)) 

      'Add some sleep to simulate a long running operation 
      System.Threading.Thread.Sleep(500) 

      'Progress report pending 
      blnReported = False 

      'increment counter 
      intCount += 1 

      'Report the progress 
      bgWorker.ReportProgress(10, intCount) 

     End If 

    End While 

End Sub 

Private Sub bgWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged 
    'Copy values to data grid 
    dgvCSVData.Rows.Add(strRow) 
    dgvCSVData.Rows(dgvCSVData.CurrentRow.Index - 1).Cells("colAmt").Value = dblAmount 

    pgbCopyProgress.Value = e.UserState 
    'Progress report finished 
    blnReported = True 

End Sub 


Private Sub bgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted 

    If e.Cancelled Or Not IsNothing(e.Error) Then 
     'Clear the data grid 
     dgvCSVData.Rows.Clear() 
    Else 
     'Progress completed 
     pgbCopyProgress.Value = 100 

     'Release txtfile 
     txtFile.Dispose() 
     btnStop.Enabled = False 
     btnStart.Enabled = True 

    End If 
End Sub 

末級

enter image description here

回答

0

在做strRow(3)strRow(4),您試圖訪問第4和數組的第五個元素。看起來陣列沒有那麼多元素。

另外,在訪問元素之前檢查數組的長度是個好習慣。

相關問題