我在辅助 DataGridView
上有一个 TabPage
,我希望在输入 TabPage
时更新网格内的数据,但我不希望处理 RowEnter
事件,除非用户实际单击了一行。似乎在 TabPage.Enter
事件触发后自动选择了网格中的第一行,所以我无法抑制它。
显示问题的代码如下。我在运行时创建了控件,因此您只需复制粘贴即可,但在实际程序中我使用了设计器。
我想看到的行为是,在选择 TabPage2
后, DataGridView
充满数据,但 TextBox1
为空,直到我单击一行。
Public Class Form1
Private WithEvents DataGridView1 As DataGridView
Private WithEvents TextBox1 As TextBox
Private WithEvents TabPage2 As TabPage
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Add controls to the form (usually I use the designer to do this)
Dim TabPage1 As New TabPage() With {.Name = "TabPage1", .Text = "TabPage1"}
TabPage2 = New TabPage() With {.Name = "TabPage2", .Text = "TabPage2"}
DataGridView1 = New DataGridView With {.Name = "DataGridView1", .SelectionMode = DataGridViewSelectionMode.FullRowSelect, .MultiSelect = False, .ReadOnly = True, .AllowUserToAddRows = False, .Size = New Size(TabPage1.Size.Width, TabPage2.Size.Height - 40), .Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom, .TabIndex = 1}
TextBox1 = New TextBox With {.Name = "TextBox1", .Top = DataGridView1.Bottom + 5, .Width = DataGridView1.Width, .Visible = True, .Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Bottom, .TabIndex = 0}
TabPage2.Controls.Add(TextBox1)
TabPage2.Controls.Add(DataGridView1)
Dim TabControl1 As New TabControl() With {.Name = "TabControl1"}
TabControl1.TabPages.Add(TabPage1)
TabControl1.TabPages.Add(TabPage2)
TabControl1.Size = Me.ClientRectangle.Size
TabControl1.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom
Me.Controls.Add(TabControl1)
End Sub
Private Sub DataGridView1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
'I would like the textbox to fill ONLY after the user has selected a row in DataGridView1.
'The problem I am having is that the first row auto-selects once I enter the tab
Dim drw As DataRow = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, DataRowView).Row
TextBox1.Text = CStr(drw(1))
End Sub
Private Sub TabPage2_Enter(sender As Object, e As EventArgs) Handles TabPage2.Enter
RefreshGrid() 'Refresh the data in the list
End Sub
Sub RefreshGrid()
'simulate a database query
DataGridView1.DataSource = Nothing
Dim dtb As New DataTable
dtb.Columns.Add("C1")
dtb.Columns.Add("C2")
dtb.Rows.Add("1", "One")
dtb.Rows.Add("2", "Two")
dtb.Rows.Add("3", "Three")
dtb.Rows.Add("4", "Four")
dtb.Rows.Add("5", "Five")
DataGridView1.DataSource = dtb
End Sub
End Class
最佳答案
来自 TabPage documentation Remarks section
由于您将 DataGridView
事件中的 TabPage.Enter
绑定(bind)到新的 DataTable
,您可以使用 DataGridView.DataBindingComplete
事件清除默认选择。
Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
TextBox1.Clear()
DataGridView1.ClearSelection()
End Sub
关于vb.net - 更新 DataGridView 但在输入 TabPage 时不选择任何行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38427404/