Search code examples
vb.netwinformsdatagridviewdatagridviewlinkcolumn

DataGridViewLinkColumn formatting


My Application is a windows form application in VB.

I have DataGridView in my Application. The seventh column is defined as DataGridViewLinkColumn when i designed the DataGridView. My Application reads the link from a table and Grid properly displays it.

I don't want my user to see the link, i want them to see a sentence like "Click here to visit" but i couldn't manage to do.

Second, when i click the link nothing happens. I know that i have to handle this in CellContentClick event but i don't know how to call the default browser directed to the link.

Thanks in advance.


Solution

  • There is no direct property in DataGridViewLinkColumn which separates the display text and url.

    To achieve your goal, you need to handle two events CellFormatting and CellContentClick. Subscribe to these events.

    In CellFormatting event handler, change the formatted value to Click here to visit. The flag FormattingApplied must be set True as this prevents further formatting of the value.

    Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
        If e.ColumnIndex = 'link column index Then
            e.Value = "Click here to visit";
            e.FormattingApplied = True;
        End If
    End Sub
    

    To open the link in the default browser, use the Process class and pass the url as argument to the Start method. Put the code in the CellContentClick event handler.

    Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
        If e.ColumnIndex = 'link column index Then
            Process.Start(dataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString());
        End If
    End Sub