英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论