gpt4 book ai didi

mysql - DataGridView 中的 List(Of T) 过载问题

转载 作者:行者123 更新时间:2023-11-29 03:36:43 24 4
gpt4 key购买 nike

我正在使用 List(Of T) 来包含我的数据库字段(invoice_id 和 item_id)并希望在 DataGridView 中显示它。首先,我将它们中的每一个都声明为一个类,然后将它们添加到我的列表中,然后在 DataGridView 中显示它,但是当我编译它时,程序没有响应。

我的猜测是我的数据库源太大了,因为当我更改源(数据库字段)时,它工作得很好。那么如何解决这个 List(Of T) 容量问题呢?

这是我的代码:

Sub view()

Dim msql2 As String
msql2 = "select invoice_id, item_id from detail"
Dim arayD As New List(Of INVOICE)

CMD2 = New MySqlCommand(msql2, conn.konek)

Try

Dim res = CMD2.ExecuteReader()
Dim INVO As INVOICE = Nothing
While res.Read()
INVO = New INVOICE
With INVO
.invoice_id = hasil2.GetString("invoice_id")
.item_id = hasil2.GetString("item_id")
End With
arayD.Add(INVO)

End While
dgv.DataSource = arayD
Catch ex As Exception
MessageBox.Show("ERROR")

End Try
End Sub

Public Class INVOICE
Private _kodeF As Integer
Public Property invoice_id() As Integer
Get
Return _kodeF
End Get
Set(ByVal value As Integer)
_kodeF = value
End Set
End Property

Private _kodeBrg As String
Public Property item_id() As String
Get
Return _kodeBrg
End Get
Set(ByVal value As String)
_kodeBrg = value
End Set
End Property
End Class

最佳答案

如果您想要一个快速的解决方案,请在 While Loop 中添加以下行:

While res.Read()
Application.DoEvents()
...
End While

或者使用如下的 BackgroundWorker:

Private WithEvents bgWorker As New System.ComponentModel.BackgroundWorker
Private arayD As New List(Of INVOICE)

Sub view()
bgWorker.RunWorkerAsync()
End Sub

Private Sub bgWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
Dim dReader As DataReader

Using YourConnection
Using YourCommand
YourConnection.Open()
dReader = YourCommand.ExecuteReader()
If dReader.HasRows Then
While dReader.Read
arayD.Add(New INVOICE With {
.invoice_id = hasil2.GetString("invoice_id"),
.item_id = hasil2.GetString("item_id")
}
)
End While
End If
End Using
End Using
End Sub

Private Sub bgWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
MsgBox("Ready to go")
dgv.DataSource = arayD
End Sub

您还可以获得结果的计数并将其与进度条一起使用。

关于mysql - DataGridView 中的 List(Of T) 过载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20602950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com