Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
Vb.net 将DataTable填充为DataGridView的源非常缓慢_Vb.net_Datagridview_Datatable_Sql Server 2012 - Fatal编程技术网

Vb.net 将DataTable填充为DataGridView的源非常缓慢

Vb.net 将DataTable填充为DataGridView的源非常缓慢,vb.net,datagridview,datatable,sql-server-2012,Vb.net,Datagridview,Datatable,Sql Server 2012,因此,这里有一个例子,我的数据库中有超过40000行。 当我启动Select*from Table时,它会在不到一秒钟的时间内获取我的结果集。 但是,当我想用这些行填充DataTable并在DataGridView中显示它时,需要花费很长时间(大约15-20秒) 为什么呢 我的代码: Public Shared Function FillDTwithSQL(ByVal StoredProc As String, ByVal table As DataTable) As DataTable

因此,这里有一个例子,我的数据库中有超过40000行。
当我启动
Select*from Table
时,它会在不到一秒钟的时间内获取我的结果集。
但是,当我想用这些行填充DataTable并在DataGridView中显示它时,需要花费很长时间(大约15-20秒)

为什么呢

我的代码:

Public Shared Function FillDTwithSQL(ByVal StoredProc As String, ByVal table As DataTable) As DataTable
        Dim cmd As SqlCommand = CreateCommand(SProcedura)
        Dim dt As New DataTable("dt")
        dt = table
        dt.Rows.Clear()
        Try
            If adoConnection.State = ConnectionState.Closed Then adoConnection.Open()
            dt.Load(cmd.ExecuteReader(CommandBehavior.Default))
        Catch ex As Exception
            MsgBox("Greska: " & ex.ToString)
            Error = True
            Error_text = ex.ToString
        End Try
        adoConnection.Close()
        Return dt
    End Function 
Public Shared Function CreateCommand(ByVal SProcedura As String) As SqlCommand
        CreateConnection(ConnectionSetup.ConnectionString)
        Dim cmd As New SqlCommand(ConnectionSetup.DataBaseName & ".dbo." & SProcedura, adoConnection)
        cmd.CommandType = CommandType.StoredProcedure
        Return cmd
    End Function 
Private sub FillDGV()
DataBaseLayer.FillDTwithSQL("SelectProc", ds_Tables.Table)
            Me.DataGridView1.DataSource = dds_Tables.Table
            Me.DataGridView1.ClearSelection()
End Sub

对于大量数据,建议使用DataGridView的虚拟模式

请查看此链接:


总结一下链接:您必须通过实现DataGridView的
CellValueRequired
事件来实现自己的数据缓存,并设置
Me.DataGridView1.VirtualMode=true

数据适配器填充(dt)或
dt.Load(读卡器)
并不慢,但在
DataGridView
中显示40000行却慢。@TimSchmelter有什么建议吗?为什么需要显示40k行,没有人会全部读取。你可以实现分页。是的,但在我看来,DataGridView的分页看起来很难看。