Question Populating a DataGridview from a Combobox selected table

davidsolerez

New member
Joined
Jul 23, 2011
Messages
2
Programming Experience
Beginner
I have found the code in this forum [RESOLVED] [2005] Populating a DataGrid from a Combobox selected table - VBForums
this is exactly what i need except i am trying to use ms access as database .It works but when I load the tables in combobox it displays other names such as MSysAccessStorage,MSysACEs,MSysComplexColumn,MSysNavPanelGroup,MSysObject
is there any i could fix the error ? I appreciate any advice


VB.NET:
Public Class Form1
    Private con As New OleDbConnection("  Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dtb.accdb;Jet OLEDB:Database Password=****  ")
    Private adapter As New OleDbDataAdapter(String.Empty, Me.con)
    Private data As DataTable
    Private Sub Form1_Load(ByVal sender As Object, _
                       ByVal e As EventArgs) Handles MyBase.Load
        con.Open()
        Me.ComboBox1.DisplayMember = "TABLE_NAME"
        Me.ComboBox1.ValueMember = "TABLE_NAME"
        Me.ComboBox1.DataSource = Me.con.GetSchema("TABLES")
        con.Close()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
                                           ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Me.ComboBox1.SelectedItem IsNot Nothing Then
            Me.data = New DataTable
            Me.adapter.SelectCommand.CommandText = String.Format("SELECT * FROM [{0}]", Me.ComboBox1.SelectedValue)
            Me.adapter.Fill(data)
            Me.DataGridView1.DataSource = Nothing
            Me.DataGridView1.Columns.Clear()
            Me.DataGridView1.DataSource = Me.data
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles Button1.Click
        If Me.data IsNot Nothing Then
            Dim builder As New OleDbCommandBuilder(Me.adapter)
            Me.adapter.Update(Me.data)
        End If
    End Sub
End Class
 
You should have a look at what restrictions are available for the TABLES collection when calling GetSchema. It will probably allow you to filter out system tables and only return user tables, although I don't know that for a fact.
 
Back
Top