Access DB テーブルに接続された VB.Net 2008 に DGV があります。DGV は読み取り専用ではありませんが、コンボ ボックスを含む 1 つを除いて読み取り専用の列でいっぱいです。コンボ ボックスを使用すると、ユーザーはその特定の行の結果を選択できます。その後、プログラムは、コンボ ボックスで選択された項目に応じて、事前に計算された値を [利益] 列にコピーします。次に、ユーザーが [保存] ボタンをクリックすると、DB が更新されます (現在は XSD の SQL メソッドを介して)。
ここまでは簡単です。
これがコードです。
Private Sub DGUserBets_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGUserBets.EditingControlShowing
Dim combo As ComboBox = CType(e.Control, ComboBox)
If (combo IsNot Nothing) Then
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
RemoveHandler combo.SelectedIndexChanged, _
New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)
// Add the event handler.
AddHandler combo.SelectedIndexChanged, _
New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)
End If
End Sub
Private Sub DGUBStake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim myStatus As ComboBox = CType(sender, ComboBox)
Dim row = DGUserBets.CurrentRow
Select Case myStatus.SelectedIndex
Case 0
row.Cells("DGUBProfit").Value = 0
// pending. no action
Case 1
row.Cells("DGUBProfit").Value = row.Cells("DGUBIfWin").Value
// win
Case 2
// loses
row.Cells("DGUBProfit").Value = row.Cells("DGUBIfLose").Value
Case 3
// void
row.Cells("DGUBProfit").Value = 0
End Select
End Sub
私が抱えている問題は、ユーザーがコンボボックスから目的の結果を選択してもEnterキーを押さず、別のコンボボックスにマウスを置いて別の行の結果を再度選択すると、最初のイベントハンドラーが切断されないように見えることです。したがって、イベントは複数回発生します。これにより、さまざまなデフォルトの MsgBox エラーが発生し、ユーザーがすべての変更を DB/exit プログラムなどにコミットしようとすると問題が発生します。
私は何をする必要がありますか?行に変更を強制的に保存するには、適切な場所で .EndEdit を実行する必要がありますか? そして、私はこれをどこに呼ぶべきですか?
ありがとうございました。