Hi team, I'm having some issues getting the results of my query to output on a data grid.
I'm using an sql query to get the data from access, which is working fine, but then I want to output it onto a datagrid. My code is as follows just for the file handling part, excuse teh scarppy-ness i'm re-learning this as my end result for the project must be a .exe lol

Dim cn As New OledbConnection()
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Wise-fps01\shared\vbDataBase.mdb"
        cn.Open()

        Dim command As OleDbCommand = New OleDbCommand("SELECT * FROM marketingDB WHERE company_name =""Opfer Logan"" ", cn)
        command.CommandType = CommandType.Text

        Dim myReader As OleDbDataReader = command.ExecuteReader()

        While myReader.Read()
            ListBox1.Items.Add(myReader("title").ToString())
            'System.Console.Write(" ")
            ListBox1.Items.Add(myReader("first_name").ToString())
            'System.Console.Write(" ")
            ListBox1.Items.Add(myReader("surname").ToString())
            'System.Console.WriteLine()

        End While

This code just outputs into seperate rows in a list box, which is all i've managed to do so far which as taken me 2 days of re-learning this syntax'y language.

So as you can see im not using sql server or anything just access. So once again, my question is how can you print the result from a query into a datagrid and keep the column names and positions of the columns the same.

If you guys can offer any help, know that it wont go un-appreciated!

Recommended Answers

All 3 Replies

I'm assuming that you have a DataGridView on your form, named DataGridView1.

Dim cn As New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Wise-fps01\shared\vbDataBase.mdb")
        cn.Open()

        Dim adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM marketingDB WHERE company_name = 'Opfer Logan'", cn)

        Dim table As New DataTable()
        adapter.Fill(table)
        cn.Close()

        DataGridView1.DataSource = table
        DataGridView.Update()

HAH! EXCELLENT! That never crossed my mind! Nice one mate! Thanks ^ 1000!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.