2014-01-21 86 views
0

我不能夠在網格視圖中顯示任何數據,它是一個空白的DataGrid 的連接是開放的,一切都很好,但沒有得到顯示的數據!我是新手訪問從MS Access數據和網格視圖中顯示

Imports System.Data.OleDb 

Class Form2 
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     con.Open() 

    Dim dvginfo As DataGridView 

    Dim table As New DataTable("Address") 

    Dim da As New OleDbDataAdapter("Select * FROM Address", con) 
    Dim dtrow As DataTable 
    da.Fill(table) 
    If table.Rows.Count = 0 Then 
     Return 
    End If 



    For Each dtrow In table.Rows 
     dvginfo.Rows.Add() 
     dvginfo.Rows(dvginfo.RowCount - 1).Cells("colFirstname").Value = dtrow("Firstname").ToString() 
     dvginfo.Rows(dvginfo.RowCount - 1).Cells("colLastname").Value = dtrow("Lastname").ToString() 
     dvginfo.Rows(dvginfo.RowCount - 1).Cells("colDOB").Value = Convert.ToDateTime(dtrow("DOB").ToString()) 
    Next 
    dvginfo.ClearSelection() 
    con.Close() 
End Sub 
End Class 
+2

昏暗dvgInfo爲DataGridView的。要麼被刪除,而應該引用表單上的網格。或者如果您在運行時創建它,則需要將控件添加到表單。例如MyForm.Controls.Add(dvgInfo) –

回答

0

嘗試......

Imports System.Data.OleDb 

Class Form2 
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
con.Open() 

Dim dvginfo As DataGridView 

Dim table As New DataTable("Address") 

Dim da As New OleDbDataAdapter("Select * FROM Address", con) 
Dim dtrow As DataTable 
da.Fill(table) 
dvginfo.DataSource = table 
con.Close() 
End Sub 
End Class