brillox
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.ICon tainer
'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.DataG rid
Friend WithEvents DataGrid2 As System.Windows.Forms.DataG rid
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataG rid
Me.DataGrid2 = New System.Windows.Forms.DataG rid
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.DataGrid2, System.ComponentModel.ISup portInitia lize).Begi nInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol or = System.Drawing.SystemColor s.ControlT ext
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.HeaderForeCol or = System.Drawing.SystemColor s.ControlT ext
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.DataGri d2)
Me.Controls.Add(Me.DataGri d1)
Me.Name = "StartUp"
Me.Text = "StartUp"
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.DataGrid2, System.ComponentModel.ISup portInitia lize).EndI nit()
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.OL EDB.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(strSelect Authors, cn)
daTitles = New OleDbDataAdapter(strSelect Titles, 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.Navig ateEventAr gs) Handles DataGrid1.Navigate
DataGrid1.SetDataBinding(d s, "Authors")
End Sub
Private Sub DataGrid2_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.Navig ateEventAr gs) Handles DataGrid2.Navigate
DataGrid1.SetDataBinding(d s, "Titles")
End Sub
End Class
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.ICon
'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.DataG
Friend WithEvents DataGrid2 As System.Windows.Forms.DataG
<System.Diagnostics.Debugg
Me.DataGrid1 = New System.Windows.Forms.DataG
Me.DataGrid2 = New System.Windows.Forms.DataG
CType(Me.DataGrid1, System.ComponentModel.ISup
CType(Me.DataGrid2, System.ComponentModel.ISup
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol
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.HeaderForeCol
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.DataGri
Me.Controls.Add(Me.DataGri
Me.Name = "StartUp"
Me.Text = "StartUp"
CType(Me.DataGrid1, System.ComponentModel.ISup
CType(Me.DataGrid2, System.ComponentModel.ISup
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.OL
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(strSelect
daTitles = New OleDbDataAdapter(strSelect
'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.Navig
DataGrid1.SetDataBinding(d
End Sub
Private Sub DataGrid2_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.Navig
DataGrid1.SetDataBinding(d
End Sub
End Class
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.OL EDB.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(strSelect Authors, cn)
daTitles = New OleDbDataAdapter(strSelect Titles, 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
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.OL
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(strSelect
daTitles = New OleDbDataAdapter(strSelect
'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
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
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
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.
If you only need to get one datatable (e.g. "AuthorAndTitle") from multiple database tables, then one dataAdapter is enough.
ASKER
SO I could write something like this:
daAuthors.Fill(ds, "Authors" , "Titles")
daAuthors.Fill(ds, "Authors" , "Titles")
No. One dataAdapter can only fill one datatable at a time.
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 ?
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("se lect * from table1; select * from table2",yourconnection)
dim ds as new dataset
da.fill(Ds)
the dataset will now be filled with 2 tables
like
dim da as new oledb.oledbdataadapter("se
dim ds as new dataset
da.fill(Ds)
the dataset will now be filled with 2 tables
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also true :-)
DataGrid1.DataSource = ds.Tables("Authors")
End Sub
Private Sub DataGrid2_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.Navig
DataGrid1.DataSource = ds.Tables("Titles")
End Sub