Link to home
Start Free TrialLog in
Avatar of brillox
brilloxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

datagrid does not display nothing

Hi I am just starting vb.NET and I am trying to connect to my access database and to display data in a datagrid.

i do not have any debug errors, but when I run it I can see the form and the two dataset, byt they are EMPTY !!

Imports System.Data
Imports System.Data.OleDb

Public Class StartUp
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents DataGrid2 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.DataGrid2 = New System.Windows.Forms.DataGrid
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(16, 16)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(328, 320)
        Me.DataGrid1.TabIndex = 0
        '
        'DataGrid2
        '
        Me.DataGrid2.DataMember = ""
        Me.DataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid2.Location = New System.Drawing.Point(368, 16)
        Me.DataGrid2.Name = "DataGrid2"
        Me.DataGrid2.Size = New System.Drawing.Size(336, 320)
        Me.DataGrid2.TabIndex = 1
        '
        'StartUp
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(712, 358)
        Me.Controls.Add(Me.DataGrid2)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "StartUp"
        Me.Text = "StartUp"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim cn As New OleDbConnection

    Dim ds As New DataSet

    Dim strSelectAuthors As String
    Dim strSelectTitles As String

    Dim daAuthors As New OleDbDataAdapter
    Dim daTitles As New OleDbDataAdapter

    Dim dtAuthors As New DataTable
    Dim dtTitles As New DataTable

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

        Dim strProvider As String
        Dim strDataSource As String
        strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
        strDataSource = "Data Source= C:\Documents and Settings\Owner\Desktop\vb tutorials\books.mdb"
        cn.ConnectionString = strProvider & strDataSource

        'open connection
        cn.Open()

        'select the dataset
        strSelectAuthors = "SELECT Authors.* FROM Authors;"
        strSelectTitles = "SELECT Titles.* FROM Titles;"

        'link tha dataAdapter to the dataSet
        daAuthors = New OleDbDataAdapter(strSelectAuthors, cn)
        daTitles = New OleDbDataAdapter(strSelectTitles, cn)

        'fill the dataAdapter with the DataSet and the tables
        daAuthors.Fill(ds, "Authors")
        daTitles.Fill(ds, "Titles")

        'close the connection
        cn.Close()

    End Sub

    Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
        DataGrid1.SetDataBinding(ds, "Authors")
    End Sub

    Private Sub DataGrid2_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid2.Navigate
        DataGrid1.SetDataBinding(ds, "Titles")
    End Sub
End Class
Avatar of amyhxu
amyhxu

   Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
        DataGrid1.DataSource = ds.Tables("Authors")
    End Sub

    Private Sub DataGrid2_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid2.Navigate
        DataGrid1.DataSource = ds.Tables("Titles")
    End Sub
actually You should set datasource in form load event:

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

        Dim strProvider As String
        Dim strDataSource As String
        strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
        strDataSource = "Data Source= C:\Documents and Settings\Owner\Desktop\vb tutorials\books.mdb"
        cn.ConnectionString = strProvider & strDataSource

        'open connection
        cn.Open()

        'select the dataset
        strSelectAuthors = "SELECT Authors.* FROM Authors;"
        strSelectTitles = "SELECT Titles.* FROM Titles;"

        'link tha dataAdapter to the dataSet
        daAuthors = New OleDbDataAdapter(strSelectAuthors, cn)
        daTitles = New OleDbDataAdapter(strSelectTitles, cn)

        'fill the dataAdapter with the DataSet and the tables
        daAuthors.Fill(ds, "Authors")
        daTitles.Fill(ds, "Titles")

        'close the connection
        cn.Close()

        DataGrid1.DataSource = ds.Tables("Authors")
        DataGrid2.DataSource = ds.Tables("Titles")

    End Sub
Avatar of brillox

ASKER

Thanks amyhxu,

it works now...

it make sense.. how I can bind something if it does not have the content. I though that the .Fill was doing the job
Avatar of brillox

ASKER

Do I really need 2 dataAdaptors to show two  different tables or there is a short way?
Yes, I would use 2 dataAdapters to fill two datatables (in your case, you have "Authors", "Titles").
If you only need to get one datatable (e.g. "AuthorAndTitle") from multiple database tables, then one dataAdapter is enough.
Avatar of brillox

ASKER

SO I could write something like this:


daAuthors.Fill(ds, "Authors" , "Titles")

No. One dataAdapter can only fill one datatable at a time.
Avatar of brillox

ASKER

< If you only need to get one datatable (e.g. "AuthorAndTitle") from multiple database tables, then one dataAdapter is enough >

Can you give me an example with my two tables ?
Amyhxu, that is not true, a dataadapter can fill multiple tabels

like

dim da as new oledb.oledbdataadapter("select * from table1; select * from table2",yourconnection)
dim ds as new dataset
da.fill(Ds)

the dataset will now be filled with 2 tables
ASKER CERTIFIED SOLUTION
Avatar of amyhxu
amyhxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also true :-)