0

我有一个填充了一些名称的datagridview,我想使用datagridview中的名称检查另一个数据库中的所有名称,并在找到匹配项时将姓氏添加到相邻的单元格中,这是我通过字符串比较方法实现的。

我的问题是,由于输入不一致,有时两个人的名字相同,一些名字没有被正确判断。

我想要的是让用户选择要么选择 Datagridview 中他们认为最匹配的名称之一,要么在新行中输入名字和姓氏。为了实现这一点,我希望程序等到用户单击 datagridview 中的某一行。

有没有办法等待这种效果?

谢谢约翰

4

3 回答 3

0

好吧,我已经找到了解决这个问题的方法,不确定它是否是更好的问题之一,所以想知道更好的方法

    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

希望它对某人有所帮助,尽管事后看来,我猜这并不是一个大问题。将寻找任何建议以使其更好。谢谢约翰

于 2010-03-18T09:22:04.927 回答
0

我是否在这里遗漏了什么,或者您不想对 DataGridView.Click 事件做出反应?然后,您可以使用 DataGridView.SelectedRows 来确保选择了一行,并对所选行执行您想要的操作。

Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView.Click

End Sub

希望这可以帮助

干杯

于 2010-03-18T09:47:10.097 回答
0

嗯,这就是我正在做的,但我将它用作一个函数,因为我希望它返回 gFlag 值。后来我决定将 gFlag 声明为全局变量,但坚持使用函数声明。干杯

于 2010-03-18T18:18:11.327 回答