We help IT Professionals succeed at work.
Get Started

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

Last Modified: 2013-11-26
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

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE