我有一个带有多个datagridviews的应用程序,现在,超过260行,我的数据网格填充速度太慢,我不知道如何缩短时间。
这是我的代码:
Public cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\BD_Estaleiro.accdb"
Public con As New OleDbConnection(cs)
Public cmd As OleDbCommand
Public dr As OleDbDataReader
Public query As String
读者功能:
Public Function executar_query_Reader(ByVal Instrucao As String) As OleDb.OleDbDataReader
Dim datareader As OleDbDataReader = Nothing
cmd = New OleDbCommand(Instrucao, con)
Try
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
datareader = cmd.ExecuteReader
Catch ex As Exception
MessageBox.Show(ex.Message) 'mensagem de erro, se aplicavel
Finally
End Try
Return datareader 'retornar os dados atraves da variavel datareader
End Function
填充数据网格:
Public Sub carregar_saidas(ByVal DG As DataGridView)
n_rows = 0
DG.Rows.Clear()
query = "Select * from tbl_saidas as s, tbl_produtos as p Where s.Produto=p.Produto Order By s.Data DESC"
dr = executar_query_Reader(query)
While dr.Read
n_rows += 1
Dim n As Integer = DG.Rows.Add()
DG.Rows.Item(n).Cells(0).Value = dr("ID")
DG.Rows.Item(n).Cells(1).Value = dr("s.Produto")
DG.Rows.Item(n).Cells(2).Value = dr("s.Quantidade")
DG.Rows.Item(n).Cells(3).Value = dr("Unidade")
DG.Rows.Item(n).Cells(4).Value = dr("Obra")
DG.Rows.Item(n).Cells(5).Value = Format(dr("Data"), "dd/MM/yyyy")
DG.Rows.Item(n).Cells(6).Value = Format$(CSng(dr("Valor_uni")), "###,####,##0.00") & " €"
DG.Rows.Item(n).Cells(7).Value = Format$(CSng(dr("Total")), "###,####,##0.00") & " €"
End While
con.Close()
End Sub
感谢您的帮助
答案 0 :(得分:3)
您希望绑定到网格中,而不是添加单独的行和列:
gcc-4.9.3
您需要在DataGridView上定义列以映射数据读取器中的字段。
您还应该考虑修改Public Sub carregar_saidas(ByVal DG As DataGridView)
Dim query As String = "Select * from tbl_saidas as s, tbl_produtos as p Where s.Produto=p.Produto Order By s.Data DESC"
Dim dr As OleDbDataReader = executar_query_Reader(query)
DG.DataSource = dr
End Sub
方法以支持参数化查询。