Smarton Smarton - 10 months ago 49 Question

Reload a Datagrid View returns Nothing

I have the a Function that loads data into from a Microsoft Access DB into a DGV as shown in code below:

Public Sub LoaddgvCombined()
'//Clear DataGrid
dgvSales.DataSource = Nothing
dgvSales.DataSource = ds

'//Establish Connection to DB
con.ConnectionString = dbProvider & dbSource
tables = ds.Tables

sql = "SELECT * FROM [tblINV_SalesRecord] WHERE CDate(adddate & ' ' &addtime) < CDate('" & selectfrm & "')) ORDER BY temID ASC"

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "tblINV_SalesRecord")

Dim view As New DataView(tables(0))
source1.DataSource = view
dgvSales.DataSource = view
dgvSales.AllowUserToAddRows = False

If dgvSales.RowCount < 1 Then
MessageBox.Show("No Sales Recorded Found From " & selectfrm)
End If

Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
MessageBox.Show("An Error Occured While Loading Sales Record ")
End Try
End Sub

It works fine the first time, but when I call the function the second time it empties the DGV but this time it does not load any Data.

Please can anyone point out what I am doing wrong.

Answer Source

The thing I noticed here is that you are passing to tables BEFORE you are getting the result of your query.

Try passing the values for tables after you fill ds.

Like this:

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "tblINV_SalesRecord")
tables = ds.Tables '<=== transfer it here

It's showing empty because at the point you are passing to tables, your ds variable is not yet filled from your query.