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
brilloxAsked:
Who is Participating?
 
amyhxuCommented:
Thanks, RonnaldBiemans. That's a good lesson :-)  Shouldn't have said no before experiment.
But from my point of view, I would still use two dataadapters to fill two tables, especially if I need to update both tables. There will be less confusion, and when error occurs, easy to check upon.
0
 
amyhxuCommented:
   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
0
 
amyhxuCommented:
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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
brilloxAuthor Commented:
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
0
 
brilloxAuthor Commented:
Do I really need 2 dataAdaptors to show two  different tables or there is a short way?
0
 
amyhxuCommented:
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.
0
 
brilloxAuthor Commented:
SO I could write something like this:


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

0
 
amyhxuCommented:
No. One dataAdapter can only fill one datatable at a time.
0
 
brilloxAuthor Commented:
< 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 ?
0
 
RonaldBiemansCommented:
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
0
 
RonaldBiemansCommented:
Also true :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.