好吧,我已经找到了解决这个问题的方法,不确定它是否是更好的问题之一,所以想知道更好的方法
Dim gFlag As Boolean = False
Function MakeChoice(ByVal Name As String, ByVal i As Integer)
Dim flag As Boolean
Dim C1, C2 As MsgBoxResult
Dim msg As String = "Select a row and press Ok to add a value to that row " & _
"Or Press cancel to Add a New Row"
'Displays and Records Users Input
C1 = MessageBox.Show(msg, Name, MessageBoxButtons.OKCancel)
Select Case C1
'If user wants to choose one of the rows in datagridview
Case MsgBoxResult.Ok
flag = True
'Scrolls to the row with closest match
dgv1.FirstDisplayedScrollingRowIndex = i
dgv1.Focus()
'Waits for gFlag to be True
Do
If gFlag = True Then
Exit Do
End If
Loop
'Resets gflag to previous state ie; False in case this function
'is accessed recursively
gFlag = False
'Finds the index of selected Row
i = dgv1.CurrentRow.Index
'Displays the message just to make sure user has chosen a right row just-in-case
C2 = MsgBox("Are you sure that """ & Name & """ is same as """ & dgv1.Rows(i).Cells(1).Value _
& "", MsgBoxStyle.OkCancel)
Select Case C2
'If user confirms then return flag to be true and hence _
'add the value in the selected row of datagridview
Case MsgBoxResult.Ok
flag = True
'If selected row is not same as the desired row then go through a recursive
'Loop
Case MsgBoxResult.Cancel
flag = MakeChoice(Name, i)
End Select
'If C1 = Cancel then add set flag to False ie add the value to a new row
Case MsgBoxResult.Cancel
flag = False
End Select
Return flag
End Function
Function GridClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.Click
'I think this part is self explanatory
gFlag = False
If sender.ToString = "System.Windows.Forms.DataGridView" Then
gFlag = True
End If
Return gFlag
End Function
希望它对某人有所帮助,尽管事后看来,我猜这并不是一个大问题。将寻找任何建议以使其更好。谢谢约翰