I am working with VB.NET in VS2013 and need an alternative to the way I am using a Main form (frmMain) search of a SQL Server db to populate a DataGridView that is populated from this search on another form (frmSearch). I am currently using a ShowDialog() Function, which is the root of my problem. It works beautifully to populate the DataGridView on frmSearch, which I then am able click in the first cell of any row, and open a third form (frmInfo) that contains more detailed information for the user. If anyone has an alternative method to use rather than the ShowDialog method, that I’m using, I would be very grateful. Here is my code:

frmMain
Code:
Public Class frmMain
    Dim cnStr As String = "Data Source=**********;Initial Catalog=TrackRFA;Persist Security Info=True;User ID=*******;Password=*******"
    Dim cn As New SqlConnection(cnStr)
    Dim com As SqlCommand
    Dim SeQL As String = ""
    Dim results As New frmSearch
    Dim hasResults As Boolean = False
    Dim dr As SqlDataReader
    Dim cmd As New SqlCommand()
    
Private Sub btnFind_Click(ByVal sender As System.Object, e As System.EventArgs) Handles btnFindR.Click
    
SeQL = "select TrackNo,ProjectNo,VendorName,ProjectDescription from " & cboArea.SelectedItem & " where TrackNo LIKE '%" & txtFindR.Text & "%'"

        'Create a New Command
        With cmd
            .CommandText = SeQL
            .Connection = cn
        End With
        cn.Open()
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        hasResults = dr.Read()
        cn.Close()

        'Check to see if a record exists
        If hasResults = False Then
            MsgBox("No Results Found for that entry")
            ReDo()
        Else

            com = New SqlCommand(SeQL, cn)

            If results.ShowDialog(com) = Nothing Then
                End
            Else
'This shows form and waits for form to close before allowing execution to advance.
                results.ShowDialog(com)
            End If
        End If

        Me.txtFindR.Text = ""
        Me.SendToBack()
    End Sub
frmSearch
Code:
Public Class frmSearch
    Public Shared RFA As String
    Public Shared Tabl As String
    Public Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles RFAGrid.CellContentClick

        If e.ColumnIndex <> 0 Then
            Exit Sub
        End If

        RFA = RTrim(CStr(RFAGrid.Rows(e.RowIndex).Cells(0).Value))
        'This alows the user to open the selected record or cancel their action
        If MsgBox("Do you want to open RFA Record ID: " & RTrim(RFA) & " ?", CType(MessageBoxButtons.YesNo, MsgBoxStyle), "View this Record?") = MsgBoxResult.Yes Then

            Tabl = Trim(CStr(frmMain.cboArea.SelectedItem))

            If Tabl = "Engineering" Or Tabl = "AssetManagement" Then
                uxOpenForm_Load(RFA, Nothing)
                frmInfo.FillForm()
            ElseIf Tabl = "StreetStormwater" Or Tabl = "UtilityBoard" Or Tabl = "WaterSewer" Then
                uxOpenForm2_Load(RFA, Nothing)
                frmInfo2.FillForm()
            ElseIf Tabl = "RFA" Then
                uxOpenForm3_Load(RFA, Nothing)
                frmInfo3.FillForm()
            ElseIf Tabl = "Vision2025" Then
                uxOpenForm4_Load(RFA, Nothing)
                frmInfo4.FillForm()
            End If
        Else
            Me.Show()
        End If

    End Sub

    Public Overloads Function ShowDialog(ByVal com As SqlCommand) As System.Windows.Forms.DialogResult
        Dim daSearch As SqlDataAdapter
        Dim showForm As Boolean = True
        Try
            'Create a new instance of a SqlDataAdapter
            daSearch = New SqlDataAdapter(com)

            'Create a command builder to generate SQL update to update the data base.
            Dim cb As New SqlCommandBuilder(daSearch)

            'Populate a New DataTable and bind it to the binding source.
            Dim table As New DataTable()

            daSearch.Fill(table)
            Me.bsData.DataSource = table

            If table.Rows.Count = 0 Then
                MsgBox("The value you have selected does match any Record!")
                End
            End If

            'Call Sub to resize the DataGridView to fit the new content
            SizeAllColumns(Nothing, Nothing)

 Catch ex As Exception
            MessageBox.Show(ex.Message)
            'If there is an exception getting the data from the database don't show the form
             showForm = False
        End Try

        If showForm Then
            'Show the form
            Return MyBase.ShowDialog()
        Else
            'Return as if the user clicked cancel
            Return DialogResult.Cancel
        End If

    End Function

    Private Sub frmSearch_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Bind the DataGridView to the BindingSource
        Me.RFAGrid.DataSource = Me.bsData
        SizeAllColumns(Nothing, Nothing)
    End Sub

    Private Sub SizeAllColumns(ByVal sender As Object, ByVal e As System.EventArgs)
        'resize the DataGridView to fit the new content
        RFAGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
    End Sub

    Private Sub uxOpenForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmInfo.TracNo.Text = RFA
        frmInfo.txtDept.Text = Tabl
        frmInfo.Show()
    End Sub
frmInfo
Code:
Public Class frmInfo
    Public Shared r As Integer
    Public LookAt As String
    Public TheDept As String
    Public Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11, Col12, Col13, Col14, Col15, Col16, Col17, Col18, Col19, Col20 As String

    Sub frmInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        LookAt = Me.TracNo.Text
        TheDept = Me.txtDept.Text
        FillForm()
    End Sub

    Public Sub FillForm()
        Dim conStr As String = "Data Source=*******;Initial Catalog=TrackRFA;Persist Security Info=True;User ID=*******;Password=*******"
        Dim linkToDB As New SqlConnection(conStr)
        Dim LookAt As String
        Dim TheDept As String
        Dim results As New frmSearch
        Dim output As New List(Of String)()
        linkToDB.Open()

        LookAt = Me.TracNo.Text
        TheDept = Me.txtDept.Text
        
        Dim sqlText As String = "select * from " & TheDept & " where TrackNo LIKE '%" & LookAt & "%'"
        Dim dataAction As New SqlCommand(sqlText, linkToDB)

        Dim scanCustomer As SqlDataReader = dataAction.ExecuteReader()

        If (scanCustomer.HasRows = True) Then
            Do While scanCustomer.Read()
                Console.WriteLine(" Your name is: " & Convert.ToString(scanCustomer(0)))
                Col1 = Convert.ToString(scanCustomer(0))
                Col2 = Convert.ToString(scanCustomer(1))
                Col3 = Convert.ToString(scanCustomer(2))
                Col4 = Convert.ToString(scanCustomer(3))
                Col5 = Convert.ToString(scanCustomer(4))
                Col6 = Convert.ToString(scanCustomer(5))
                Col7 = Convert.ToString(scanCustomer(6))
                Col8 = Convert.ToString(scanCustomer(7))
                Col9 = Convert.ToString(scanCustomer(8))
                Col10 = Convert.ToString(scanCustomer(9))
                Col11 = Convert.ToString(scanCustomer(10))
                Col12 = Convert.ToString(scanCustomer(11))
                Col13 = Convert.ToString(scanCustomer(12))
                Col14 = Convert.ToString(scanCustomer(13))
                Col15 = Convert.ToString(scanCustomer(14))
                Col16 = Convert.ToString(scanCustomer(15))
                Col17 = Convert.ToString(scanCustomer(16))
                Col18 = Convert.ToString(scanCustomer(17))
                Col19 = Convert.ToString(scanCustomer(18))
                Col20 = Convert.ToString(scanCustomer(19))

            Loop
        End If

        Me.TracNo.Text = LookAt
        Me.ProjNo.Text = Col2
        Me.ContNo.Text = Col3
        Me.EaseNo.Text = Col4
        Me.ssNo.Text = Col5
        Me.OrdNo.Text = Col6
        Me.baNo.Text = Col7
        Me.FunNo.Text = Col8
        Me.ResNo.Text = Col9
        Me.DeeNo.Text = Col10
        Me.VenNam.Text = Col11
        Me.txtAmt.Text = Col12
        Me.ProDesc.Text = Col13
        Me.ProBeDat.Text = Col14
        Me.FilNam.Text = Col15
        Me.txtRMUA.Text = Col16
        Me.txtTMUA.Text = Col17
        Me.txtTARE.Text = Col18
        Me.txtMemo.Text = Col19
        Me.MayApp.Text = Col20

        scanCustomer.Close()
        linkToDB.Close()
        frmSearch.Owner.Close()
    End Sub
I have tried other methods and failed until I used the OpenDialog, which is a real pain but it works to a certain extent. The problem I am having with the ShowDialog is closing forms behind me, and trying use the print feature to open other forms to print a CrystalReportViewer. It keeps going back to the “If showForm Then” in the ShowDialog Function on the frmSearch form, and errors on the Return statement like it’s in an infinite loop or recursion. I would really like to find an alternative to the ShowDialog and populate the DataGridView differently on ‘frmSearch’. Thank you