SiteMap  | About    

Adding ViewLink to DataGridView in VB.NET

The DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types. We can add hyperlink in the column of a DataGridView , the column type contains cells of type DataGridViewLinkCell and renders the text in the cell to look like a hyperlink. Link columns are not generated automatically when data binding a DataGridView control. To use link columns, you must create them manually and add them to the collection returned by the Columns property.

The following program shows how to add a hyperlink in a column of DataGridView control.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Product ID"
        DataGridView1.Columns(1).Name = "Product Name"
        DataGridView1.Columns(2).Name = "Product_Price"

        Dim row As String() = New String() {"1", "Product 1", "1000"}
        row = New String() {"2", "Product 2", "2000"}
        row = New String() {"3", "Product 3", "3000"}
        row = New String() {"4", "Product 4", "4000"}

        Dim lnk As New DataGridViewLinkColumn()
        lnk.HeaderText = "Link Data"
        lnk.Name = ""
        lnk.Text = ""
        lnk.UseColumnTextForLinkValue = True

    End Sub
End Class (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.