Question How do I access a DataTable on another form?

computer guy

Member
Joined
Feb 12, 2008
Messages
18
Programming Experience
Beginner
Hello all,

I have a DataSet(dssongs) with a DataTable(dtsongs) on a form(songsForm).

I am adding rows to the dataTable using the following code:
VB.NET:
            Dim dr As DataRow = songsForm.dtSongs.NewRow
            dr.Item("colSongFile") = file
            dr.Item("colSongTitle") = ""
            dr.Item("colSongAlbum") = ""
            dr.Item("colSongArtist") = ""
            dr.Item("colSongStatus") = "Queued"
            songsForm.dtSongs.Rows.Add(dr)

If I then do:
VB.NET:
MsgBox("Count: " & dtSongs.Rows.Count)

I get "Count: 1", however if I go to another form and do:

VB.NET:
MsgBox("Count: " & songsForm.dtSongs.Rows.Count)

I get "Count: 0".

How do I refer to a DataTable on another form?


Thank you :)
 
First you need an instance to reference. When you do songsForm.dtSongs.Rows.Count you are accessing the 'blueprint' for the songsForm not the actual instance of songsForm that has data in it.

For instance the blue prints of a house will not tell you the color of Suzie's room, because the blueprint does only knows there is a room, not that it is Suzie's or what color it is. In your case songsForm knows that there is a DataTable called dtSongs, but the blueprint doesn't know what data should be in it. You will need to get a reference to the songsForm then you can access the DataTable data.

How do you get the reference you want/need. There are differents ways.

Use OpenForms to find it if you know it will be only instance of the form.

VB.NET:
Public Module modUtil
    Public Shared Function IsFormAlreadyOpen(ByVal PassForm As System.Windows.Forms.Form) As Boolean

        Dim ReturnBool As Boolean = False

        For Each frm As System.Windows.Forms.Form In My.Application.OpenForms
            If frm.GetType() Is PassForm.GetType() Then
                ReturnBool = True
                Exit For
            End If
        Next

        Return ReturnBool

    End Function

    Public Shared Function GetInstanceOfForm(ByVal PassForm As System.Windows.Forms.Form) As System.Windows.Forms.Form

        Dim FormInstance As System.Windows.Forms.Form = Nothing

        If IsFormAlreadyOpen(PassForm) Then

            FormInstance = My.Application.OpenForms(PassForm.Name)
        End If

        Return FormInstance
    End Function
End Module

'Form 1
Public Class frm1
     Public dtSongs As DataTable

     'the datatable is filled in the form somewhere

End Class

'Form 3
Public Class frm3
     
     Public Sub GetForm1DataTable()
          Dim frmGeneric As System.Windows.Forms.Form = GetInstanceOfForm(frm1)

           If frmGeneric IsNot Nothing Then
                
                'need to cast it to the correct 'blueprint'
                 Dim frmSpecific As frm1= DirectCast(frmGeneric, frm1)
                 'frmSpecific in this case becomes a local instance of the frm1 blueprint.

                 'Access the DataTable
                 frmSpecific.dtSongs

        End If
          
     End Sub

End Class

Access it through the owner of the form

VB.NET:
Form 1
Public Class frm1
     Public dtSongs As DataTable

     'the datatable is filled in the form somewhere

     Public Sub OpenForm3
          Dim frmTemp As New frm3

          frmTemp.Show(Me)
     End Sub
End Class

'Form 3
Public Class frm3
     Public Sub GetForm1DataTable()
          'the owner of this instance of frm3 is an instance of frm1
          Dim frmSpecific As frm1 = DirectCast(Me.Owner, frm1)
          
          'so we can cast that into a variable and then access it's members      

          frmSpecific.dtSongs          
          
     End Sub
End Class

Outright pass it to the other form that needs to access it.

VB.NET:
Public Class frm1
     Public dtSongs As DataTable

     'the datatable is filled in the form somewhere

     Public Sub OpenForm3
          Dim frmTemp As New frm3(Me)

          frmTemp.Show()
     End Sub
End Class

'Form 3
Public Class frm3
     Dim myInstanceOffrm1 As frm1

     Public Sub New(ByVal Passfrm1 As frm1)

          myInstanceOffrm1 = Passfrm1

     End Sub

     Public Sub GetForm1DataTable()
          'Access the DataTable
          myInstanceOffrm1.dtSongs        
          
     End Sub
End Class

I hope this helps.
 
Back
Top