2016-11-21 37 views
1

我有如下圖所示的代碼加載從Microsoft Access數據庫到DGV數據到一個功能:刷新數據網格視圖返回Nothing

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

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

     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") 
     con.Close() 

     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 

它正常工作的第一次,但是當我打電話該功能第二次清空DGV,但這次它不加載任何數據。

請任何人都可以指出我做錯了什麼。

回答

1

我在這裏注意到的事情是,您正在傳遞到tables之前您正在得到您的查詢結果。

嘗試在填寫ds後傳遞tables的值。

像這樣:

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

它顯示因爲要傳遞到tables點空,你ds變量尚未從查詢填充。

+0

非常感謝,你只是讓我的一天,它的工作。 – Smarton