Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在填充数据表时引发事件_C#_.net_Vb.net_Winforms_Visual Studio 2008 - Fatal编程技术网

C# 在填充数据表时引发事件

C# 在填充数据表时引发事件,c#,.net,vb.net,winforms,visual-studio-2008,C#,.net,Vb.net,Winforms,Visual Studio 2008,我有一个表单,我必须在其中填充DataGrid。此DataGrid的源来自另一个类 我需要通过在获取Datatable的类中引发事件来填充DataGrid Imports System.IO Public Class ExcelReader Private WithEvents tmrRead As New Timer Dim fullpath As String = "" Public Sub ExcelReader() tmrRead.Inte

我有一个表单,我必须在其中填充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
Imports System.IO
公共课优秀读者
Private with events tmr作为新计时器读取
将完整路径变暗为字符串=“”
公共Sub-ExcelReader()
t平均间隔=2000
tmrRead.Start()
端接头
Public Sub TimerTick(ByVal sender作为对象,ByVal e作为System.EventArgs)处理tmrRead.Tick
Dim DT作为新数据表
Dim路径为String=Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory.ToString).ToString+“\ExcelHotReader\”
Dim file1作为字符串()=System.IO.Directory.GetFiles(路径“*.xls”)
Dim file2作为字符串()=System.IO.Directory.GetFiles(路径“*.xlsx”)
如果file1.Count为0或file2.Count为0,则
tmrRead.Stop()
如果结束
如果file1.Count为0,则
完整路径=文件1(0)。ToString
如果结束
如果file2.Count为0,则
完整路径=文件2(0)。ToString
如果结束
Dim DT作为新数据表
Dim cn As System.Data.OleDb.OleDb连接
Dim cmd作为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(“从[Sheet1$]中选择*”,cn)
cn.Open()
指令填充(DT)
cn.Close()
端接头
末级

cn.Close()之后,应使用Datatable引发事件。表单需要捕获此事件以填充DataGrid。

在ExcelReader类中添加事件声明,并在代码末尾使用RaiseEvent调用它

Public Class ExcelReader
    Public Event DataTableLoaded(ByVal dt As DataTable)
    .......

        Using cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.12.0;.......")
        Using cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", cn)
            cn.Open()
            cmd.Fill(DT)
            RaiseEvent DataTableLoaded(DT)
        End Using
        End Using
    ....
End Class
然后调用代码用关键字WithEvents(全局)声明ExcelReader类的一个实例

Public Dim WithEvents readerTableFromExcel = New ExcelReader()
最后是处理事件的方法(事件接收器)的声明

最后,我将使用不同的方法开始读取Excel文件(例如,类似乎比TimerTick事件更合适)


如果您需要加深对事件的了解,这似乎是对需要了解的重要内容的一个很好的总结

旁注,如果您想同时支持“xls”和“xlsx”,您需要ACE.12.0 oledb provider而不是Jet.4.0如果您想在cn.Close()之后调用函数,为什么不直接调用它呢?事件与具有“动态行为”的控件关联,否则将无法跟踪。但是cn.Close()是相当静态的:它写在一行代码中;因此,在这一行后面直接调用给定的方法并不困难。+1,因为您的答案非常详细,为OP的问题提供了一个优雅的解决方案。但我看不出让事情变得如此复杂的动机:在cn.Close()之后简单调用所需函数而不涉及任何事件有什么问题?@varocabas我不知道。我认为只有OP才能回答你的问题。但是,也许,看到它使用了TimerTick事件,他可能不希望在加载表时它的gui保持阻塞状态。
Public Sub DataTableFromExcel(ByVal dt As System.Data.DataTable) _
           Handles readerTableFromExcel.DataTableLoaded
    MsgBox("Table loaded")
End Sub