DataGridView 加载时调用的 RowLeave 事件
RowLeave event called when DataGridView loads
我正在使用数据网格视图将 Access 数据库填充到 VB.net 中的表单。当用户连续更改值时,我想执行某些操作(validation/updating 数据库)。我想等到该行失去焦点才能执行更改。我正在使用 RowLeave 事件来执行任务。问题是当加载带有 datagridview 的表单时,它会自动运行 RowLeave 事件。
如何加载表单而不是 运行 的 RowLeave 事件?是什么导致在加载表单时触发 RowLeave 事件?
编辑:这是我的代码的一部分:
Public Sub loadGradeForm(ByRef temp As Teacher)
currTeacher = temp
Me.showGrades() 'Populating the datagridview (GradeGridView)
Me.Show()
'After the form loads, it automatically goes to RowLeave when I go through debugging
End Sub
Private Sub showGrades()
GradeGridView.DataSource = Nothing
GradeGridView.Rows.Clear()
GradeGridView.Columns.Clear()
GradeGridView.AutoGenerateColumns = False
GradeGridView.AutoSize = True
GradeGridView.DataSource = currTeacher.ListOfClasses()
Dim column0 As DataGridViewColumn = New DataGridViewTextBoxColumn()
column0.DataPropertyName = "Class"
column0.Name = "Class"
GradeGridView.Columns.Add(column0)
Dim column As DataGridViewColumn = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Grade"
column.Name = "Grade"
GradeGridView.Columns.Add(column)
Dim column1 As DataGridViewColumn = New DataGridViewTextBoxColumn()
column1.DataPropertyName = "Semester"
column1.Name = "Semester"
GradeGridView.Columns.Add(column1)
End Sub
Private Sub GradeGridView_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles GradeGridView.RowLeave
MsgBox(e.RowIndex)
'This is just a test to see if RowLeave is triggered
End Sub
由于您的 DataGridView 中填充了行,因此如果 DataGridView 失去焦点,这些行肯定会以编程方式保留。
为避免您的代码在表单将行加载到 DataGridView 时触发,我想到了两个解决方案:
- 仅在
Me.IsLoaded
为 True
时执行 GradeGridView_RowLeave()
方法中的逻辑。
- 在数据初始加载后添加事件处理程序,方法是删除
GradeGridView_RowLeave(sender As Object, e As DataGridViewCellEventArgs)
Handles GradeGridView.RowLeave
和在 showGrades()
方法的底部添加行 AddHandler GradeGridView.RowLeave, AddressOf GradeGridView_RowLeave
。
当然,上述解决方案仅在您仅在程序开始时加载一次数据时才有效。如果您需要再次加载数据,例如用户单击 Update-Button,我建议您向 class 添加一个 LoadingMode
-boolean 标志,这将在加载发生之前设置为 true,在加载完成时设置为 false。在执行 GradeGridView_RowLeave()
的逻辑之前,可以类似于 Me.IsLoaded
检查此布尔变量。
我正在使用数据网格视图将 Access 数据库填充到 VB.net 中的表单。当用户连续更改值时,我想执行某些操作(validation/updating 数据库)。我想等到该行失去焦点才能执行更改。我正在使用 RowLeave 事件来执行任务。问题是当加载带有 datagridview 的表单时,它会自动运行 RowLeave 事件。
如何加载表单而不是 运行 的 RowLeave 事件?是什么导致在加载表单时触发 RowLeave 事件?
编辑:这是我的代码的一部分:
Public Sub loadGradeForm(ByRef temp As Teacher)
currTeacher = temp
Me.showGrades() 'Populating the datagridview (GradeGridView)
Me.Show()
'After the form loads, it automatically goes to RowLeave when I go through debugging
End Sub
Private Sub showGrades()
GradeGridView.DataSource = Nothing
GradeGridView.Rows.Clear()
GradeGridView.Columns.Clear()
GradeGridView.AutoGenerateColumns = False
GradeGridView.AutoSize = True
GradeGridView.DataSource = currTeacher.ListOfClasses()
Dim column0 As DataGridViewColumn = New DataGridViewTextBoxColumn()
column0.DataPropertyName = "Class"
column0.Name = "Class"
GradeGridView.Columns.Add(column0)
Dim column As DataGridViewColumn = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Grade"
column.Name = "Grade"
GradeGridView.Columns.Add(column)
Dim column1 As DataGridViewColumn = New DataGridViewTextBoxColumn()
column1.DataPropertyName = "Semester"
column1.Name = "Semester"
GradeGridView.Columns.Add(column1)
End Sub
Private Sub GradeGridView_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles GradeGridView.RowLeave
MsgBox(e.RowIndex)
'This is just a test to see if RowLeave is triggered
End Sub
由于您的 DataGridView 中填充了行,因此如果 DataGridView 失去焦点,这些行肯定会以编程方式保留。
为避免您的代码在表单将行加载到 DataGridView 时触发,我想到了两个解决方案:
- 仅在
Me.IsLoaded
为True
时执行GradeGridView_RowLeave()
方法中的逻辑。 - 在数据初始加载后添加事件处理程序,方法是删除
GradeGridView_RowLeave(sender As Object, e As DataGridViewCellEventArgs)
Handles GradeGridView.RowLeave
和在showGrades()
方法的底部添加行AddHandler GradeGridView.RowLeave, AddressOf GradeGridView_RowLeave
。
当然,上述解决方案仅在您仅在程序开始时加载一次数据时才有效。如果您需要再次加载数据,例如用户单击 Update-Button,我建议您向 class 添加一个 LoadingMode
-boolean 标志,这将在加载发生之前设置为 true,在加载完成时设置为 false。在执行 GradeGridView_RowLeave()
的逻辑之前,可以类似于 Me.IsLoaded
检查此布尔变量。