Vb.net 将DataTable填充为DataGridView的源非常缓慢
因此,这里有一个例子,我的数据库中有超过40000行。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
当我启动
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的分页看起来很难看。