Sub Tabs()
Dim shtWork1 As Short
Dim dbpath As String = "db\... "
Dim conn As New OleDbConnection(dbpath ... )
For shtWork1 = 1 To shtMaxTabs
Dim TabPageAll As New TabPage
Try
'define DataGridViews
Dim DataGridView1 As New DataGridView
DataGridView1.Parent = TabPageAll
.
.
.
DataGridView1.AutoGenerateColumns = True
TabControl1.TabPages.Add(TabPageAll)
Catch ex as ...
Message
Finally
Close()
End Try
Next shtWork1
End Sub
'网格在TabPages上创建并且工作正常。 但是,如果我点击一个单元格,它应该给我执行一些其他代码的可能性,即 在特定的TabPage上填充文本框。 任何想法都会被贬低。 TIA Frank
答案 0 :(得分:0)
添加DataGridView时只需添加一个事件处理程序
AddHandler DataGridView1.CellClick, AddressOf cellClickHandler
这是一个示例处理程序代码
Private Sub cellClickHandler(sender As Object, e As DataGridViewCellEventArgs)
MessageBox.Show(String.Format("Row '{0}', Col '{1}'", e.RowIndex, e.ColumnIndex))
End Sub
修改强>
完整的解决方案
为新的winforms应用添加标签控件。只需将此代码添加到Form1类。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tabPageAll As New TabPage()
Dim dataGridView1 As New DataGridView()
Dim d As New Dictionary(Of String, String)() _
From {{"A", "1"}, {"B", "2"}, {"C", "3"}}
dataGridView1.AutoGenerateColumns = True
dataGridView1.DataSource = _
(From ds In d
Select New With {.Key = ds.Key, .Value = ds.Value}).ToArray()
AddHandler dataGridView1.CellClick, AddressOf cellClickHandler
tabPageAll.Controls.Add(dataGridView1)
TabControl1.TabPages.Add(tabPageAll)
TabControl1.SelectedTab = tabPageAll
End Sub
Private Sub cellClickHandler(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs)
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
' DataGridView.Rows() works just fine here
Dim selectedRow = CType(sender, DataGridView).Rows(e.RowIndex)
End If
End Sub