Access object properties in another class

Chong

Regular
Joined
Apr 4, 2003
Messages
79
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
 
try this ...

in Form1 ( the form that holds the datagrid you want to fill ) :
Visual Basic:
    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 ) :
Visual Basic:
    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
 
Back
Top