views:

16

answers:

3

I have a datagridview populated with some Names, I want to perform a check of all the Name in another database with the Names in datagridview and add the Surnames to the adjacent cell when a match is found, this I am achieving by string compare method.

My Problem is that due to typing inconsistencies & at times with two people having same names some of the names are not being adjudged properly.

What I want is to give user the choice to either choose one of the names present in Datagridview which they consider is best match or enter both First Name & Surname in a new row. To achieve this I want the program to wait until user has clicked on one of the rows in the datagridview.

Is there a way to wait to this effect?

Thanks Jhon

A: 

Well I have sort of found a workaround this problem am not sure if it is one of the better ones so would like to know any better way

    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

Hope it helps someone though on hindsight it was not such a big issue I guess. Would be looking for any suggestions to make it better. Thanks Jhon

Jhon
A: 

Am i missing something here or would not you not want to react to the DataGridView.Click event? Then you can use the DataGridView.SelectedRows to make sure that there is a row selected, and do what you want with the rows selected.

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

End Sub

Hope this helps

Cheers

Ben
A: 

Well that's what I am doing but I am using it as a function since I wanted it to return the gFlag value. Later on I decided to declare gFlag as global variable but persisted with function declaration. Cheers

Jhon