我正在使用List(Of T)来包含我的数据库字段(invoice\u id和item\u 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

此外,您还可以获得结果的计数,并将其与progressbar一起使用。

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

10-10 13:45