将DataTable填充为DataGridView的源非常慢

时间:2014-07-25 15:58:00

标签: vb.net datagridview datatable sql-server-2012

所以,这是我的数据库中有超过40.000行的情况 当我开始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

1 个答案:

答案 0 :(得分:2)

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

请看一下这个链接:How to: Implement Virtual Mode in the Windows Forms DataGridView Control

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