Binding Data to DataGridView in VB.Net

Posted by Peter on Stack Overflow See other posts from Stack Overflow or by Peter
Published on 2010-04-05T18:04:24Z Indexed on 2010/04/05 20:53 UTC
Read the original article Hit count: 547

Filed under:
|
|
|

Hi,

I have a bit of code which loads data from a stored procedure in MS SQL Server and then loads the data to a datagridview, which works fine. What i want is for the code that connects / loads the data to sit in my Database Class and then everything associated with the datagridview to be stored in my Form but i am having problems passing the contents of the bindingsource over to the Form from the Database Class.

Form1

Public Class Form1

Dim myDatabaseObj As New Class1()
Dim bindingSource1 As New BindingSource()
Dim connectString As New SqlConnection
Dim objDataAdapter As New SqlDataAdapter
Dim table As New DataTable()
Dim tabletest As New DataTable()

Private Sub loadCompanyList()
    Try
        Me.dgv_CompanyList.DataSource = Me.bindingSource1
        getCompanyList()

    Catch ex As NullReferenceException
    End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)       
Handles MyBase.Load
    loadCompanyList()
End Sub

End Class

Database Class

When i try place the getCompanyList() in a class and then create a new object that references the Form() it does not seem to return any value from the table to the MyForm.BindingSource1.Datasource meaning my datagridview displays not data.

.....
Private Sub getCompanyList()
    Try

        Dim myForm as new Form()

        connect_Transaction_Database()
        objDataAdapter.SelectCommand = New SqlCommand()
        objDataAdapter.SelectCommand.Connection = connectString
        objDataAdapter.SelectCommand.CommandText = "sp_GetCompanyList"
        objDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim commandBuilder As New SqlCommandBuilder(Me.objDataAdapter)

        Dim table As New DataTable()
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        Me.objDataAdapter.Fill(table)
        **MyForm.bindingSource1.DataSource = table**

    Catch ex As DataException
        MsgBox(ex.Message)
    Catch ex As NullReferenceException
        MsgBox(ex.Message)
    End Try

    disconnect_Transaction_Database()

End Sub

If anyone could help.

Thank you.

Peter

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about vb