Link to home
Start Free TrialLog in
Avatar of monica73174
monica73174

asked on

VB.Net datagridview event handler error

I have two datagridviews on my form.
Datagrid one loads some data.
The user clicks on a row and the second datagridview is supposed to display the records based on the information in the first datagridview.

this doesn't seem to work and I get an error that says Operation cannot be performed in this event handler.

Here is my code that loads the first data grid

 Sub fillUnmatched()
        Dim i As Integer = 0
        Dim cnUnmatched As New SqlConnection(connectionString)
        Dim sqlString As String = "SELECT orgAdvisorBenID, original_name, advisor_name, beneficial_name, institution_id " + _
                                  "FROM advisor_beneficial " + _
                                  "WHERE original_name IS NOT NULL AND institution_id IS NULL OR " + _
                                  "institution_id = 0 " + _
                                  "ORDER BY original_name"

        cnUnmatched.Open()

        Dim cmd As New SqlCommand(sqlString, cnUnmatched)
        Dim rdr As SqlDataReader = cmd.ExecuteReader
        While rdr.Read()
            frmUnMatched.dtgUnmatched.Rows.Add()
            frmUnMatched.dtgUnmatched.Rows(i).Cells("ID").Value = rdr("orgAdvisorBenID")
            frmUnMatched.dtgUnmatched.Rows(i).Cells("Title").Value = rdr("original_name")
            i += 1
        End While
        cnUnmatched.Close()
    End Sub

Here is my code that triggers the load of the second datagridview:

    Private Sub dtgUnmatched_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgUnmatched.CellContentClick, dtgSuggestions.RowEnter
        data.fillSuggestions(dtgUnmatched.Rows(e.RowIndex).Cells(0).Value)
    End Sub

Here is my code that fills the second datagrid - The error happens here when I add a row.

Sub fillSuggestions(ByVal ItemID As String)
        Dim i As Integer = 0
        Dim fillSuggestions As New SqlConnection(connectionString + ";Asynchronous Processing=true;MultipleActiveResultSets=true")
        Dim strSQLString As String = "SELECT UnMatchedID, orgAdvisorBenID, advisor, beneficial " + _
                                     " FROM suggestedMatches " + _
                                     " Where orgAdvisorBenID = '" + ItemID + "' " + _
                                     " ORDER BY advisor"

        frmUnMatched.dtgSuggestions.SuspendLayout()

        fillSuggestions.Open()


        Dim cmd As New SqlCommand(strSQLString, fillSuggestions)
        Dim rdr As SqlDataReader = cmd.ExecuteReader
        While rdr.Read()
            frmUnMatched.dtgSuggestions.Rows.Add()
            frmUnMatched.dtgSuggestions.Rows(i).Cells(0).Value = rdr("UnMatchedID")
            frmUnMatched.dtgSuggestions.Rows(i).Cells(1).Value = rdr("orgAdvisorBenID")
            frmUnMatched.dtgSuggestions.Rows(i).Cells(2).Value = rdr("advisor")
            frmUnMatched.dtgSuggestions.Rows(i).Cells(3).Value = rdr("beneficial")
            i = i + 1
        End While

        frmUnMatched.dtgSuggestions.ResumeLayout()
        fillSuggestions.Close()

    End Sub
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Is your second datagridview data bound. If so you can not add it that way.

jpaulino
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of monica73174
monica73174

ASKER

The second grid is unbound.