vb.neturldatagridviewwebview2datagridviewcolumn

How to Double Click the DataGridView column then it appears in WebView2 in vb.net


I want to double click the "Name" column of the datagridview then it appears in webview2 based on the "url" column.

Thanks

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Me.Resize, AddressOf Form_Resize
        AddHandler WebView.NavigationStarting, AddressOf EnsureHttps
        InitializeAsync()
        Dim t As New DataTable()
        t.Columns.Add("id")
        t.Columns.Add("Name")
        t.Columns.Add("url")
        t.Rows.Add(New Object() {"1", "Philips APAC Centre", "https://www.google.com/search?tbs=lf:1,lf_ui:4&tbm=lcl&q=philips+singapore&rflfq=1&num=10&rldimm=6082032048092871869&ved=2ahUKEwjAuLeM2un-AhWAzzgGHe7qDswQu9QIegQIRhAL#rlfi=hd:;si:3450890534334536328,l,ChFwaGlsaXBzIHNpbmdhcG9yZSIDiAEBSN_D87CqroCACFojEAAYABgBIhFwaGlsaXBzIHNpbmdhcG9yZSoECAIQADICZW6SARZjb25zdW1lcl9hZHZpY2VfY2VudGVyqgFVCgwvZy8xeW16enJzbGQQASoLIgdwaGlsaXBzKAAyHxABIhspDALHL2gj5FH_XQXCrrvZukYbjrVUmSjB-CYyFRACIhFwaGlsaXBzIHNpbmdhcG9yZQ;mv:[[1.4505105,103.89954859999999],[1.2679569,103.79753199999999]]"})
        t.Rows.Add(New Object() {"2", "Philips Lighting (Light Lab)", "https://www.google.com/search?tbs=lf:1,lf_ui:4&tbm=lcl&q=philips+singapore&rflfq=1&num=10&rldimm=6082032048092871869&ved=2ahUKEwjAuLeM2un-AhWAzzgGHe7qDswQu9QIegQIRhAL#rlfi=hd:;si:6082032048092871869,l,ChFwaGlsaXBzIHNpbmdhcG9yZSIDiAEBSL-c_5rngICACFojEAAYABgBIhFwaGlsaXBzIHNpbmdhcG9yZSoECAIQADICZW6SARVsaWdodGluZ19tYW51ZmFjdHVyZXKqAVUKDC9nLzF5bXp6cnNsZBABKgsiB3BoaWxpcHMoADIfEAEiGykMAscvaCPkUf9dBcKuu9m6RhuOtVSZKMH4JjIVEAIiEXBoaWxpcHMgc2luZ2Fwb3Jl;mv:[[1.4505105,103.89954859999999],[1.2679569,103.79753199999999]]"})
        DataGridView1.DataSource = t
        DataGridView1.Columns("url").Visible = False
    End Sub
    Private Sub Form_Resize(ByVal sender As Object, ByVal e As EventArgs)
        WebView.Size = Me.ClientSize - New System.Drawing.Size(WebView.Location)
    End Sub
    Private Sub EnsureHttps(ByVal sender As Object, ByVal args As CoreWebView2NavigationStartingEventArgs)
        Dim uri As String = args.Uri
        If Not uri.StartsWith("https://") Then
            WebView.CoreWebView2.ExecuteScriptAsync($"alert('{url} is not safe, try an https link')")
            args.Cancel = True
        End If
    End Sub
    Private Async Sub InitializeAsync()
        Await WebView.EnsureCoreWebView2Async(Nothing)
        Await WebView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.postMessage(window.document.URL);")
    End Sub

Solution

  • My advice would be to add the columns you want in grid in the designer. Add a text box column for id and a link column for Name, then set AutoGenerateColumns to False in code so no extra columns are generated. Note that you need to set the DataPropertyName of each column so it knows how to bind to the data source. You can then handle the CellContentClick event of the grid and maths the column index to the index of the link column.

    I would also suggest that you add a BindingSource in the designer and then bind your DataTable via that. You can then get the current record from its Current property.

    myBindingSource.DataSource = myDataTable
    myDataGridView.DataSource = myBindingSource
    

    and:

    Private Sub myDataGridView_CellContentClick(...) Handles myDataGridView.CellContentClick
        If e.ColumnIndex = linkColumnIndex Then
            Dim url = CStr(DirectCast(myBindingSource.Current, DataRowView)("url"))
    
            'Use url as desirde.
        End If
    End Sub