这里有一个类似的问题Why didn't trigger the CellEndEdit event of DataGridView所以请不要告诉我它已经回答了。 :-)该解决方案解决了网格中的单击,而不是从行中的最后一个单元格中跳出来。
我将数据输入到datagridview中的新行。当我离开行时,我检查该行是否是新的。如果是新行,我将数据添加到数据库中。但是,当我跳出最后一个单元格时,RowLeave将触发,行中的最后一个单元格为空。其他单元格显示数据但不显示最后一个。
我确认,当Tabed退出最后一个字段时,LeaveRow会在CellEndEdit之前触发。
如果我在最后一个单元格中输入数据并单击上一个单元格,那么离开该行,它将从最后一个单元格中捕获数据。
如何在RowLeave事件触发之前强制保存我在最后一个单元格中输入的值?我在RowLeave事件中保存新行。
这是RowLeave事件的代码
Private Sub dgVisa_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles dgVisa.RowLeave
Dim row As DataGridViewRow = dgVisa.CurrentRow
If dgVisa.IsCurrentRowDirty Then
MsgBox(row.Cells(0).FormattedValue)
MsgBox(row.Cells(1).FormattedValue)
MsgBox(row.Cells(2).FormattedValue)
MsgBox(row.Cells(3).FormattedValue)
MsgBox(row.Cells(4).FormattedValue)
MsgBox(row.Cells(5).FormattedValue)
If gbolUserAddedRow = True Then
Dim AddRecord As New GetMSCADPaymentDetailsTableAdapters.uspMSGetMSCADBankDetailsByCreditCardTypeIdentTableAdapter
AddRecord.Insert(#9/1/2018#,
1,
5,
1,
4)
AddRecord.Insert(CDate(row.Cells(1).FormattedValue),
1,
CDbl(row.Cells(3).FormattedValue),
CDbl(row.Cells(4).FormattedValue),
CDbl(row.Cells(5).FormattedValue))
gbolUserAddedRow = False
End If
End If
End Sub
我不知道这是否只是一种解决方法,但我确实从解决问题的另一个问题中找到了一些东西。
我把dgVisa.EndEdit()放在上面显示的代码的开头。它会强制EndCellEdit在其余代码之前触发。
如果有人有更优雅的答案,请告诉我。
谢谢!乔