DataGridView中的List(Of T)重载问题

时间:2013-12-16 03:24:12

标签: mysql vb.net winforms datagridview

我正在使用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

1 个答案:

答案 0 :(得分:0)

如果您想要快速解决方案,请在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

此外,您可以获取结果的计数并将其与进度条一起使用。