Looping through Datagridview with BackgroundWorker in vb.net

Shiko 1 Reputation point
2021-05-21T14:13:29.183+00:00

Hi
I am using Datagridview with BackgroundWorker in vb.net to simultaneously check the network connectivity status of list of IP addresses but I am getting an error states that Index was outside the bounds of the array after the 1st successful ping.

Any idea what could be causing it and how I can overcome it?

98660-error1-index-was-outside-the-bounds-of-the-array.png

98610-error1-code-index-was-outside-the-bounds-of-the-ar.png

98647-error-index-was-outside-the-bounds-of-the-array.png

Developer technologies VB
{count} votes

2 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-05-21T21:00:30.617+00:00

    Is your DoWork accessing the DataGridView directly? You are not supposed to do that. Windows has always not supported accessing a window by any thread that did not create the window. It can work sometimes but Microsoft does not guarantee it will work. You can create a collection (such as a List) for the DataGridView (IP) data and bind the collection to the DataGridView then the DoWork can use the collection. Descriptions of binding a collection to a DataGridView is integrated in the DataGridView Control Overview. I suggest using a BindingSource component as mentioned there. Also see Data Binding - Windows Forms .NET Framework. I am sure that Karen has examples of binding a DataGridView to a collection.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-05-21T17:36:38.887+00:00

    My guess is the array has less elements than rows in the DataGridView e.g.

    Dim someArray As String() = {"", "", ""}
    For rowIndex As Integer = 0 To DataGridView1.RowCount - 1
        DataGridView1.Rows(rowIndex).Cells("Column1").Value = someArray(rowIndex)
        Console.WriteLine(DataGridView1.Rows(rowIndex).Cells("Column1").Value)
    Next
    

    While this would work as there are equal elements in the array as rows in the DataGridView

    Dim someArray As String() = {"1", "2", "3", "4", "5", "6", "7", "8"}
    For rowIndex As Integer = 0 To DataGridView1.RowCount - 1
        DataGridView1.Rows(rowIndex).Cells("Column1").Value = someArray(rowIndex)
        Console.WriteLine(DataGridView1.Rows(rowIndex).Cells("Column1").Value)
    Next
    

    And this would assert we are in range

    Dim someArray As String() = {"1", "2", "3", "4", "5"}
    For rowIndex As Integer = 0 To DataGridView1.RowCount - 1
    
        If rowIndex < someArray.Length Then
            DataGridView1.Rows(rowIndex).Cells("Column1").Value = someArray(rowIndex)
            Console.WriteLine(DataGridView1.Rows(rowIndex).Cells("Column1").Value)
    
        End If
    Next
    

    Demo

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"Other"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"CountMe"})
            DataGridView1.Rows.Add(New Object() {"Other"})
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim someArray As String() = {"1", "2", "3", "4", "5"}
            For rowIndex As Integer = 0 To DataGridView1.RowCount - 1
    
                If rowIndex < someArray.Length Then
                    DataGridView1.Rows(rowIndex).Cells("Column1").Value = someArray(rowIndex)
                    Console.WriteLine(DataGridView1.Rows(rowIndex).Cells("Column1").Value)
    
                End If
            Next
        End Sub
    End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.