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.
Looping through Datagridview with BackgroundWorker in vb.net
Shiko
1
Reputation point
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?
Developer technologies VB
2,892 questions
2 answers
Sort by: Most helpful
-
-
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