VB. NET How to Get Value of Cell in Selected Rows in a DataGridView

I am writing a basic Windows Form application (WebTest) that does a web search using the Bing WebService, based on whatever query text the user enters.  I am displaying the returned results in a DataGridView with three columns -- one for the title of the web page, one for the description, and one for the URL.

When the user selects a row and clicks the Browser button, a second form comes up and displays the web page (using a WebBrowser item on the form).  This works fine if the user has only selected one row.  However, if the user has selected multiple rows I would like it to open up an instance of that second form for each selected row.  So if the user selects three rows and clicks the Browser button, three new forms would open up, each displaying a separate web page.

I've attached the code that works for one selected row.  I would appreciate any help getting this to work for multiple selected rows.
Imports System
Imports System.Xml

Imports WebTest.BingWS

Public Class frmMain

    Const AppId As String = "MyBingAppID"

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim WS As New BingWS.LiveSearchService
            Dim request As SearchRequest = BuildRequest()
            Dim response As SearchResponse = WS.Search(request)
        Catch ex As System.Web.Services.Protocols.SoapException
        Catch ex As System.Net.WebException
        End Try
    End Sub

    Shared Function BuildRequest() As SearchRequest
        Dim request As New SearchRequest

        With request
            .AppId = AppId
            .Query = frmMain.txtQuery.Text
            .Sources = New SourceType() {SourceType.Web}

            .Version = "2.0"
            .Market = "en-us"
            .Adult = AdultOption.Moderate
            .AdultSpecified = True
            .Options = New SearchOption() {SearchOption.EnableHighlighting}

            .Web = New WebRequest
            With .Web
                .Count = frmMain.txtCount.Text
                .CountSpecified = True
                .Offset = 0
                .OffsetSpecified = True
                .Options = New WebSearchOption() _
                { _
                    WebSearchOption.DisableHostCollapsing, _
                    WebSearchOption.DisableQueryAlterations _
            End With
        End With

        Return request
    End Function

    Shared Sub DisplayResponse(ByVal response As SearchResponse)

        Dim builder1 As New System.Text.StringBuilder
        Dim builder2 As New System.Text.StringBuilder
        Dim builder3 As New System.Text.StringBuilder
        Dim result As WebResult
        Dim S1 As String = ""
        Dim S1Temp As String = ""
        Dim S1Temp1 As String = ""
        Dim S2 As String = ""
        Dim S2Temp As String = ""
        Dim S2Temp2 As String = ""
        Dim S3 As String = ""
        Dim S3Temp As String = ""
        Dim S3Temp3 As String = ""
            For Each result In response.Web.Results
                With result
                    builder1.Length = 0
                    S1Temp = builder1.ToString

                    builder2.Length = 0
                    S2Temp = builder2.ToString

                    builder3.Length = 0
                    S3Temp = builder3.ToString
                End With
                S1Temp1 = S1Temp.Replace(Char.ConvertFromUtf32(&HE000), "")
                S1 = S1Temp1.Replace(Char.ConvertFromUtf32(&HE001), "")
                S2Temp2 = S2Temp.Replace(Char.ConvertFromUtf32(&HE000), "")
                S2 = S2Temp2.Replace(Char.ConvertFromUtf32(&HE001), "")
                S3Temp3 = S3Temp.Replace(Char.ConvertFromUtf32(&HE000), "")
                S3 = S3Temp3.Replace(Char.ConvertFromUtf32(&HE001), "")
                frmMain.DGV.Rows.Add(New Object() {S1, S2, S3})
            MessageBox.Show("No results found.")
            Exit Sub
        End Try
    End Sub

    Shared Sub DisplayErrors(ByVal errorDetails As XmlNode)
        Dim nsmgr As New XmlNamespaceManager( _
        nsmgr.AddNamespace( _
            "api", _

        Dim errors As XmlNodeList = errorDetails.SelectNodes( _
            "./api:Errors/api:Error", _

        If (Not errors Is Nothing) Then
            Dim [error] As XmlNode
            For Each [error] In errors
                Dim detail As XmlNode
                For Each detail In [error].ChildNodes
                    MessageBox.Show("Errors: " & detail.Name & detail.InnerText)
        End If
    End Sub

    Private Sub btnBrowser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowser.Click
        Dim WBForm As New frmWebBrowser
        Dim dgvRows As DataGridViewSelectedRowCollection = DGV.SelectedRows
        WBForm.webResults.Url = New Uri(dgvRows.Item(0).Cells(2).Value.ToString)
    End Sub
End Class

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Coralie CrumrineCommented:
You should just need to loop through all the rows like this.
Private Sub btnBrowser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowser.Click
        Dim WBForm As New frmWebBrowser
        'Dim dgvRows As DataGridViewSelectedRowCollection = DGV.SelectedRows
        Dim lCount as int32 = DGV.SelectedRows.Count

        For X as int32 = 0 to lCount - 1
        WBForm.webResults.Url = New Uri(dgvRows.Item(X).Cells(2).Value.ToString)
    End Sub

Open in new window

Pettitte96Author Commented:
I put this code in but only one instance of the second form comes up.  It comes up with the web page of the first row I selected.

I put it into debug and watched it go through -- it did loop through twice (I selected two rows), and the value of X in the New Uri(dgvRows.Item(X).Cells(2).Value.ToString) was 0 on the first pass, 1 on the second, as I would expect.  However, the second form never appeared.

I don't believe that I have put any restrictions in place on the number of forms that can show.
Coralie CrumrineCommented:
Ah!  You will probably need to create a new instance of the form for each row.

For X as int32 = 0 to lCount - 1
        WBForm = new frmWebBrowser
        WBForm.webResults.Url = New Uri(dgvRows.Item(X).Cells(2).Value.ToString)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pettitte96Author Commented:
Perfect!  That did it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.