Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I tried to check status of list of hostnames a ports in datagrid columns but I have a problem with built a code - at this row
For Each GridViewRow As GridViewRow In GridView2.Rows
This row is skipped.

What I have tried:

<pre>Dim dz = New DataTable()
        dz.Columns.Add(New DataColumn("Hostname", GetType(String)))
        dz.Columns.Add(New DataColumn("Port", GetType(String)))
        dz.Columns.Add(New DataColumn("Status", GetType(String)))
        Dim row = dz.NewRow()

        row("Hostname") = "host1"
        row("Port") = 7777
        dz.Rows.Add(row)

        row = dz.NewRow()
        row("Hostname") = "host2"
        row("Port") = 8888
        dz.Rows.Add(row)
      
        For Each GridViewRow As GridViewRow In GridView2.Rows
            Dim ping = New Ping()
            Dim result As PingReply = Nothing
            Dim status As IPStatus
            Dim hos As String = GridViewRow.Cells(1).Text.ToString()
            Dim po As String = GridViewRow.Cells(2).Text.ToString()
           
            Try
                result = ping.Send(hos, po)
                status = result.Status
                If status = IPStatus.DestinationHostUnreachable Then

                Else
                    row("Status") = result.Status

                End If
            Catch ex As Exception
               

            End Try
        Next
        dz.AcceptChanges()
        GridView2.DataSource = dz
        GridView2.DataBind()
Posted
Updated 12-Jul-21 2:13am

1 solution

Your loop is trying to execute before you have connected the DataTable to the DataGridView. So there are no rows to read.
 
Share this answer
 
Comments
Richard MacCutchan 12-Jul-21 8:57am    
Your code is iterating through the rows of GridView2, but it adds the status value to the row object, which is a reference to the last row in the DataTable. You need to use the GridViewRow* which is the reference to the current row in GridView2.
Sorry, my mistake. What you actually need to do is to iterate through the rows of the DataTable and update the status field there. That should then be reflected in the Gridview via the data binding.
Member 15251555 13-Jul-21 2:44am    
thank you :) i did it :)
Richard MacCutchan 13-Jul-21 3:58am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900