Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a datagride in, let's say frmMain, but how do I access this datagrid's properties in another class, let's say clsComputer? When I'm creating a public function to load the data from a datbase mdb file into frmMain's datagrid1, how do I do that? Here's what I have tried access in the clsComputer class:

 

Public Class clsComputer

Private Shared mdbPath As String = Application.StartupPath & "\Computers.mdb"

Dim clsMain As frmMain

Dim CompDG As DataGrid = clsMain.dgComputer

Public Function ComputerDG()

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & mdbPath

Dim strSQL As String = "SELECT * FROM Computer"

Dim compDataAdapter As New OleDbDataAdapter(strSQL, strConn)

Dim compDataSet As New DataSet()

Dim ts As New DataGridTableStyle()

 

compDataSet.Clear()

compDataAdapter.Fill(compDataSet, "CompDevices")

CompDG.DataSource = compDataSet.Tables!CompDevices

 

'GET CURRENCY MANAGER

Dim cm As CurrencyManager

 

cm = CType(clsMain.BindingContext(compDataSet.Tables!CompDevices), CurrencyManager)

 

'CREATE DATAGRIDSTYLES AND MODIFY

 

ts = New DataGridTableStyle(cm)

ts.GridColumnStyles(0).HeaderText = "Serial#"

ts.GridColumnStyles(0).Alignment = HorizontalAlignment.Center

ts.GridColumnStyles(0).ReadOnly() = True

ts.GridColumnStyles(1).Alignment = HorizontalAlignment.Center

ts.GridColumnStyles(1).ReadOnly() = True

ts.GridColumnStyles(2).Alignment = HorizontalAlignment.Center

ts.GridColumnStyles(2).ReadOnly() = True

ts.GridColumnStyles(3).HeaderText = "Date Purchased"

ts.GridColumnStyles(3).Alignment = HorizontalAlignment.Center

ts.GridColumnStyles(3).ReadOnly() = True

ts.GridColumnStyles(3).Width() = 100

CompDG.TableStyles.Add(ts)

End Function

End Class

But I received an error:

An unhandled exception of type �System.NullReferenceException� occurred in Devices.exe

 

Additional information: Object reference not set to an instance of an object.

If I declared Dim clsMain As frmMain as Dim clsMain As New frmMain then I don't receive any error, but the problem now is that the data does not load from the database mdb file into the datagrid in the frmMain form.

 

Many thanks in advance!

 

ljCharlie

  • Leaders
Posted

try this ...

 

in Form1 ( the form that holds the datagrid you want to fill ) :

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim frm2 As New clsComputer()
       AddOwnedForm(frm2)
       frm2.Show()
   End Sub

 

in form2 ( whatever the class is you want to use to open the database ) :

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Try
           Dim Connection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:/MyDb.mdb"
           Dim Command As String = "SELECT * FROM Table1"

           Dim objConnection As New OleDb.OleDbConnection(Connection) '/// make a new connection.
           Dim objCommand As New OleDb.OleDbCommand(Command, objConnection)
           Dim objAdaptor As OleDb.OleDbDataAdapter
           objAdaptor = New OleDb.OleDbDataAdapter(objCommand)
           Dim objDataSet As New DataSet()
           objAdaptor.Fill(objDataSet, "Table1")

           '////////// try this below//////////////////////////
           Dim frmMain As Form1 = Owner '//////// try this <<<<<
           frmMain.DataGrid1.DataSource = objDataSet.Tables("Table1") '/// add the table to a datagrid.
           '//////////////////////////////////////////
           objDataSet.Dispose()
           objAdaptor.Dispose()
           objCommand.Dispose()
           objConnection.Close()

       Catch ex As Exception
           MessageBox.Show(ex.Message)
       End Try
   End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...