I am using a datagridview to populate an Access Database to a form in VB.net. I want to perform certain actions (validation/updating database) when a user changes values in a row. I want to wait until the row loses focus to perform the changes. I am using the RowLeave event to perform the tasks. The problem is that when the form with the datagridview loads, it automatically runs the RowLeave event.
How do I go about having the form load and not have the RowLeave event from running? What is causing the RowLeave event to be triggered upon loading of the form?
EDIT: This is part of my code:
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
As your DataGridView is populated with the rows, the rows are sure left programmatically if the DataGridView looses the focus.
To avoid your code will fire when the form is loading the rows to the DataGridView, I could think of two solutions:
GradeGridView_RowLeave()
-method whenMe.IsLoaded
isTrue
.GradeGridView_RowLeave(sender As Object, e As DataGridViewCellEventArgs)
Handles GradeGridView.RowLeave
and add a the lineAddHandler GradeGridView.RowLeave, AddressOf GradeGridView_RowLeave
to the bottom of yourshowGrades()
-method.Of course the above stated solutions only work if you load your data only once, at the beginning of the program. If you need to load the data again, like if the user clicks an Update-Button, I would recommend adding a
LoadingMode
-boolean flag to your class, that would be set to true before the loading happens, and false when the loading is complete. This boolean variable could be checked similar toMe.IsLoaded
before the logic ofGradeGridView_RowLeave()
is executed.