?
Solved

datagrid does not display nothing

Posted on 2005-04-20
11
Medium Priority
?
245 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:brillox
  • 5
  • 4
  • 2
11 Comments
 
LVL 14

Expert Comment

by:amyhxu
ID: 13829050
   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
 
LVL 14

Expert Comment

by:amyhxu
ID: 13829076
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
 

Author Comment

by:brillox
ID: 13829098
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:brillox
ID: 13829111
Do I really need 2 dataAdaptors to show two  different tables or there is a short way?
0
 
LVL 14

Expert Comment

by:amyhxu
ID: 13829271
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
 

Author Comment

by:brillox
ID: 13829289
SO I could write something like this:


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

0
 
LVL 14

Expert Comment

by:amyhxu
ID: 13829585
No. One dataAdapter can only fill one datatable at a time.
0
 

Author Comment

by:brillox
ID: 13831159
< 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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13833315
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
 
LVL 14

Accepted Solution

by:
amyhxu earned 2000 total points
ID: 13833630
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13833686
Also true :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question