Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to access a filled data table in another form containing a data gird view control.But it doesn't fetch data. i defined it on a module inside a function
C#
da = New OleDbDataAdapter(cmd)
result = New DataTable da.Fill(result)

on another form
C#
If radmaterial.Checked = True 
Then 
dtgResult.DataSource = Module2.result dtgResult.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells 
End If
Posted
Updated 25-Jul-13 22:55pm
v2
Comments
berrymaria 26-Jul-13 4:59am    
A better way would be for Form1 to pass the DataTable to Form2 when it creates it.
berrymaria 26-Jul-13 5:00am    
Form1 contains the datagridview control, so with the datatable, Pass the DataTable to Form2.

check this out
you answer is here hit ones[
 
Share this answer
 
First of all, if you want to pass data between forms, please, use SearchBox[^] to find proper articles. Most of them uses C#, but it's easy to understand and convert C# code to VB.NET.

Secondly, to undestand how to access to objects created in other procedure/function, please, read about: Variable and Method Scope in Microsoft .NET[^]

Finally, i would suggest you to use Interfaces[^] to "communicate" between objects.
 
Share this answer
 
Here is what i do on vb.net Pseudo Code
Declare a shared(public variable) datatable and a binding source.
Ex


VB
'Shared Class
Public Class SharedClass
 Public Shared Dt as new Datatable
 Public Shared Bs as new BindingSource
End Class

'On your form1
Public Class Form1
 Sub LoadData()
  SharedClass.Dt.Clear
  SharedClass.Dt = (Your Source...)
  SharedClass.Bs.DataSource = SharedClass.Dt
  Datagridview1.Datasource = SharedClass.Bs
 End Sub

End Class

'On your form2
Public Class Form2
 Sub LoadData()
  Datagridview2.DataSource = SharedClass.Bs
 End Sub

End Class


Using BindingSource will Syncronize the two datagridviews. Whatever row you selected on form1 will reflect on form2. if you add,edit delete on form2 datagridview and call sharedClass.Dt.AcceptChanges method will reflect on Form1 datagridview.
 
Share this answer
 
v3
I directly accessed from the records from the db and loaded them on a certain data table.
This is all done using a procedure that accepts a string that holds a SQL statement.
VB
LoadData(ByVal s As String)

And when ever a user chooses a certain item that he wants to see,he clicks on a certain option btn and he clicks.Under this button the procedure along with the sql statement is called.
VB
If radmaterial.Checked = True Then
            cmd = "SELECT * FROM mtrcost"
            LoadData(cmd)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900