VB.NET - how to access the datatable from another form in vb.net

Asked By rajeshwari balu on 11-Nov-11 06:56 AM
Hi friends,

      I created an application for retail shop. In sales form, I use the Datatable and add the values or columns of the datatable into the datagrid now i want to create a bill when clicking the bill button, so how i create the report with the datatable. when the bill button clicks, another form named as bill form is displayed in that form the report is viewed. For that i want the datatable from the sales form and use it in this bill form.

how to access the datatable from another form and create the report pls give some logic
byeeeeee
Kirtan Patel replied to rajeshwari balu on 11-Nov-11 07:15 AM
lets say you have two forms Form1 and Form2 you want to pass DataTable to Form2
then you need to create constructor in Form2 that acceept DataTable as Argument and when calling Form2 you can pass DataTable as Argument
 
Form2 : Create Constructor
  
Public Class Form2
  Private _dt As DataTable
 
  Public Sub New(ByVal dt As DataTable)
    InitializeComponent()
    _dt = dt
  End Sub
  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
  End Sub
End Class
 
Call Form1 with Passing Datatable
 
Public Class Form1
 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim yourdatatable As New DataTable
    Dim f2 As Form2 = New Form2(yourdatatable)
    f2.Show()
  End Sub
End Class
 
Now form2 can use _dt that will contain datapassed from Form 1
 
Devil Scorpio replied to rajeshwari balu on 12-Nov-11 05:48 AM
Hi,

Here is the code to access the datatable from another form in 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

Refer the following website for three different ways to do the same
http://www.vbdotnetforums.com/windows-forms/43075-how-do-i-access-datatable-another-form.html