user2934164 user2934164 - 1 year ago 97 Question

DatagridView not loading from DataTable

I'm trying to load data from a database and then presenting it in a DatagridView. I've tried different combinations using different classes like an SQLDataAdapter, DataBinding as per suggestions of various online websites.

However nothing works..

This is my code:

Dim sql As String

sql = "select "
sql = sql & " dbo.tblMvMovimentos.Sigla, dbo.tblMvMovimentos.numero,dbo.tblMvMovimentos.chkdigit,"
sql = sql & " CodTipocontentor,Dim, dbo.tblMvMovimentos.CodArmador,CodDebito,"
sql = sql & " DataEntrada, EstadoCarga, PesoBascula, "
sql = sql & " FrigoTemperatura, FrigoLigado, Observacoes"

sql = sql & " from tblMvMovimentos INNER JOIN dbo.tblCtContentores ON"
sql = sql & " dbo.tblMvMovimentos.Sigla = dbo.tblCtContentores.Sigla AND"
sql = sql & " dbo.tblMvMovimentos.Numero = dbo.tblCtContentores.Numero"

Dim rs As ADODB.Recordset

Consulta(sql, rs)

da = New OleDb.OleDbDataAdapter
ds = New DataTable()
da.Fill(ds, rs)

With DataGridView1
.AutoGenerateColumns = False
.DataSource = ds
End With


Consulta is a function which deals with queries to the db. I've written those message boxes to see if data was actually being loaded and it is both to the recordset and to the DataTable. However the last one, refering to the datagrid returns 0 and the datagridview appears all gray. Can anyone point me in the right direction?

Answer Source
With DataGridView1
    .AutoGenerateColumns = True
    .DataSource = ds
End With

When you set AutoGenerateColumns = False, you have to define the columns yourself. If you set that to True, it will determine the columns based on the Datasource.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download