我有一个表单,我必须在其中填充 DataGrid。此 DataGrid 的源来自另一个类。
我需要通过在其中获取 Datatable 的类中的引发事件来填充 DataGrid。
Imports System.IO
Public Class ExcelReader
Private WithEvents tmrRead As New Timer
Dim fullpath As String = ""
Public Sub ExcelReader()
tmrRead.Interval = 2000
tmrRead.Start()
End Sub
Public Sub TimerTick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrRead.Tick
Dim DT As New DataTable
Dim path As String = Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory).ToString).ToString + "\ExcelHotReader\"
Dim file1 As String() = System.IO.Directory.GetFiles(path, "*.xls")
Dim file2 As String() = System.IO.Directory.GetFiles(path, "*.xlsx")
If file1.Count <> 0 Or file2.Count <> 0 Then
tmrRead.Stop()
End If
If file1.Count <> 0 Then
fullpath = file1(0).ToString
End If
If file2.Count <> 0 Then
fullpath = file2(0).ToString
End If
Dim DT As New DataTable
Dim cn As System.Data.OleDb.OleDbConnection
Dim cmd As System.Data.OleDb.OleDbDataAdapter
cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & "data source=" & fullpath & ";Extended Properties=Excel 8.0;")
cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", cn)
cn.Open()
cmd.Fill(DT)
cn.Close()
End Sub
End Class
在 cn.Close() 之后,应该使用 Datatable 引发事件。此事件需要由表单捕获以填充 DataGrid。