Need to display folder location as a hyperlink in a data grid view (Windows app)

Posted on 2012-09-10
Last Modified: 2012-09-11
Hi Experts,

I have populated a datagridview by binding it to a dataview created from an SQL query at run time.

One of the fields is titled "File Folder" and shows a folder location on our server.  I would like to display this column as a hyperlink so that users can simply click it to open explorer and open the folder.

Here is my code so far:

Dim cmd As New SqlClient.SqlCommand(SqlLine$, myConnection)
cmd.CommandTimeout = 120
da.SelectCommand = cmd
DS = New DataSet
da.Fill(DS, "MyTable")
dv = New DataView(DS.Tables("MyTable"))
DGV1.DataSource = dv

Dim links As New DataGridViewLinkColumn()
With links
            .Name = "File Folder"
            .UseColumnTextForLinkValue = True
            .HeaderText = "File Folder2"
            .DataPropertyName = DGV1.Columns("File Folder").DataPropertyName.ToString
            .ActiveLinkColor = Color.White
            .LinkBehavior = LinkBehavior.SystemDefault
            .LinkColor = Color.Blue
            .TrackVisitedState = True
            .VisitedLinkColor = Color.YellowGreen
End With

Open in new window

This code creates a column on my datagridview titled "File Folder2", but the cells are blank.

What am I missing ?

Thanks for your help !!
Question by:Steve5140

    Author Comment

    Well, I changed .UseColumnTextForLinkValue =   from "True" to "False" and now the hyperlink displays.

    I don't understand that, but for now I will accept it.

    Next problem is that clicking on the link doesn't do anything.
    LVL 13

    Accepted Solution

    Implement CellContentClick event

    Private Sub DGV1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
          If TypeOf DGV1.Columns(e.ColumnIndex) Is DataGridViewLinkColumn Then
                Dim folderName As String = DGV1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()

                Process.Start(String.Format("{0}", folderName))
          End If
    End Sub

    Author Comment

    OK, that seems to work well.  I changed it slightly to:

    If DGV1.Columns(e.ColumnIndex).HeaderText.ToString = "File Folder" Then
    End If

    Open in new window

    Just in case I add a second DataGridViewLinkColumn someday in the future.

    Can you explain why setting .UseColumnTextForLinkValue to false made the text appear in the cell ?  That just doesn't make sense to me ?

    I will happily award the points to you for your answer !!
    LVL 13

    Expert Comment

    by:Naman Goel
    I never tried this property so it was never set for me and so it was always taking default value for me.
     But it seems that if you make this property true text will be treated as undelink URL for that Hyperlink.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now