dcsimg
Results 1 to 10 of 10
  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    open hyperlink in a cell in datagridview

    Hey,

    I am using a Datagridview and it has a column named documents which is a hyperlink to another file in the shared hard drive,
    I tried with this:
    Code:
     If e.ColumnIndex = 1 Then ' Index of Link Column
                Dim strURL As String
                strURL = Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
                If strURL.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase) Or strURL.StartsWith("\\", StringComparison.CurrentCultureIgnoreCase) Then
                    Process.Start(strURL)
                End If
            End If
        End Sub
    but it doesn't work with the hyperlinks, it worked only with the links starts with http:// or \\

    can help please?

  2. #2
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    438

    Re: open hyperlink in a cell in datagridview

    To get you going on URL this should work
    Code:
            Dim URLStr As String = "domain.com"
            Process.Start(If(URLStr.ToUpper.Contains("HTTP"), URLStr, "http://" & URLStr))
    If you have UNC paths you'll need to figure in more conditions.

  3. #3
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    77

    Re: open hyperlink in a cell in datagridview

    I didn't knew it existed, but after 2 seconds on google i came across the DataGridViewLinkColumn.

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    I don't suppose you are using that, are you?

    if so the msdn article explains how to configure the column, so it will handle the link for you.

    Code:
    Private Sub AddLinkColumn()
    
        Dim links As New DataGridViewLinkColumn()
        With links
            .UseColumnTextForLinkValue = True
            .HeaderText = ColumnName.ReportsTo.ToString()
            .DataPropertyName = ColumnName.ReportsTo.ToString()
            .ActiveLinkColor = Color.White
            .LinkBehavior = LinkBehavior.SystemDefault
            .LinkColor = Color.Blue
            .TrackVisitedState = True
            .VisitedLinkColor = Color.YellowGreen
        End With
        DataGridView1.Columns.Add(links)
    End Sub
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: open hyperlink in a cell in datagridview

    Quote Originally Posted by kpmc View Post
    To get you going on URL this should work
    Code:
            Dim URLStr As String = "domain.com"
            Process.Start(If(URLStr.ToUpper.Contains("HTTP"), URLStr, "http://" & URLStr))
    If you have UNC paths you'll need to figure in more conditions.
    my hyperlinks starts with \\ because it is on a shared drive not on the WWW

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: open hyperlink in a cell in datagridview

    Quote Originally Posted by Goggy View Post
    I didn't knew it existed, but after 2 seconds on google i came across the DataGridViewLinkColumn.

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    I don't suppose you are using that, are you?

    if so the msdn article explains how to configure the column, so it will handle the link for you.

    Code:
    Private Sub AddLinkColumn()
    
        Dim links As New DataGridViewLinkColumn()
        With links
            .UseColumnTextForLinkValue = True
            .HeaderText = ColumnName.ReportsTo.ToString()
            .DataPropertyName = ColumnName.ReportsTo.ToString()
            .ActiveLinkColor = Color.White
            .LinkBehavior = LinkBehavior.SystemDefault
            .LinkColor = Color.Blue
            .TrackVisitedState = True
            .VisitedLinkColor = Color.YellowGreen
        End With
        DataGridView1.Columns.Add(links)
    End Sub
    I did and found this, but it did not work with me

  6. #6
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    438

    Re: open hyperlink in a cell in datagridview

    my hyperlinks starts with \\ because it is on a shared drive not on the WWW
    Do all your values in this column start with "\\"?
    This will treat any string without a preceding url or unc as a url
    Code:
            Dim URLStr As String = "domain.com"
            If URLStr.Substring(0, 2) = "\\" OrElse URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start(URLStr)
            ElseIf Not URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start("http://" & URLStr)
            Else
            End If

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: open hyperlink in a cell in datagridview

    Quote Originally Posted by kpmc View Post
    Do all your values in this column start with "\\"?
    This will treat any string without a preceding url or unc as a url
    Code:
            Dim URLStr As String = "domain.com"
            If URLStr.Substring(0, 2) = "\\" OrElse URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start(URLStr)
            ElseIf Not URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start("http://" & URLStr)
            Else
            End If
    still does not work!
    when I Used your code it adds hhtp:// before my link and that causes fail to open page,
    I have changed to ''\\ to load the page I want and it says Additional information: The network path was not found

  8. #8
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    438

    Re: open hyperlink in a cell in datagridview

    Code:
            Dim URLStr As String = "domain.com"
            If URLStr.Substring(0, 2) = "\\" OrElse URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start(URLStr)
            ElseIf Not URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start("http://" & URLStr) 'change this to  Process.Start("\\" & URLStr)
            Else
            End If
    I have changed to ''\\ to load the page I want and it says Additional information: The network path was not found
    just realized you did that.
    if you paste the string in a run prompt im sure you get the same result?

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: open hyperlink in a cell in datagridview

    Quote Originally Posted by kpmc View Post
    Code:
            Dim URLStr As String = "domain.com"
            If URLStr.Substring(0, 2) = "\\" OrElse URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start(URLStr)
            ElseIf Not URLStr.ToUpper.Substring(0, 4) = "HTTP" Then
                Process.Start("http://" & URLStr) 'change this to  Process.Start("\\" & URLStr)
            Else
            End If
    just realized you did that.
    if you paste the string in a run prompt im sure you get the same result?
    it works with me now, but it loads the domain.com and does not open my link in the cell!!
    I don't want to have specific link because I different links on the cells
    Last edited by vbuser99; Dec 5th, 2017 at 10:28 PM.

  10. #10
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    438

    Re: open hyperlink in a cell in datagridview

    Judging from your initial post I would have assumed that you know how to get the value from the cell.

    here is one out of 100 ways to do it.
    Code:
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            With DataGridView1
                If .CurrentCell.OwningColumn.Name = "URLColumn" Then
                    If .CurrentCell.Value.ToString.Length > 0 Then 'or check NULL
                        If .CurrentCell.Value.ToString.Substring(0, 2) = "\\" OrElse .CurrentCell.Value.ToString.ToUpper.Substring(0, 4) = "HTTP" Then
                            Process.Start(.CurrentCell.Value.ToString)
                        ElseIf Not .CurrentCell.Value.ToString.ToUpper.Substring(0, 4) = "HTTP" Then
                            Process.Start("http://" & .CurrentCell.Value.ToString)
                        Else
                        End If
                    End If
                End If
            End With
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width