如何在vb.net中双击DataGridView列,然后它会显示在WebView2中。

huangapple go评论51阅读模式
英文:

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

问题

我想要双击 datagridview 的 "Name" 列,然后它会在 webview2 中显示,基于 "url" 列。

谢谢
英文:

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

答案1

得分: 0

我的建议是在设计中添加想要的网格列。为id添加一个文本框列,为Name添加一个链接列,然后在代码中将AutoGenerateColumns设置为False,以便不生成额外的列。请注意,您需要为每列设置DataPropertyName以便它知道如何绑定到数据源。然后,您可以处理网格的CellContentClick事件,并将列索引与链接列的索引匹配。

我还建议您在设计中添加一个BindingSource,然后通过它绑定您的DataTable。然后,可以通过其Current属性获取当前记录。

myBindingSource.DataSource = myDataTable
myDataGridView.DataSource = myBindingSource

以及:

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

        '使用url,如所需。
    End If
End Sub
英文:

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

huangapple
  • 本文由 发表于 2023年5月10日 11:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定