如何使用包含5列的数据表以5列填充Listview?

问题描述:

我的下面的代码无效,它产生此错误"对象引用未设置为对象的实例。"它应该做的是从我的数据表中检索值并填充我的列表视图。任何想法有什么问题?

My code below is not working, its producing this error "Object reference not set to an instance of an object." What it should do is retrieve values from my datatable and populate my listview. Any ideas what is wrong?

Sub fetch_DATABASE(ByVal getclass As String)
        'load database into datatable into listview

        Dim loadThisClass As String
        loadThisclass = getclass

        'load class data from MS access db
        Dim cnn As New OleDb.OleDbConnection
        Dim cmd As New OleDb.OleDbCommand
        Dim dataAdapter As New OleDb.OleDbDataAdapter
        cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=QuizDB.accdb"
        cnn.Open()

        With cmd
            .Connection = cnn
            .CommandText = "SELECT * FROM " & loadThisClass
        End With

        'create datatable
        Dim MyDataTable As New DataTable
        dataAdapter.SelectCommand = cmd

        'use dataAdapter values to fill the DataTable
        dataAdapter.Fill(MyDataTable)

        'loop through each row of the datatable and add to ListItem
        Dim MyDataRow As DataRow
        For i = 0 To MyDataTable.Rows.Count
            MyDataRow = MyDataTable.Rows(i)
            LVITEMS.SubItems.Add(MyDataRow("UserName").ToString())
            LVITEMS.SubItems.Add(MyDataRow("Score1").ToString())
            LVITEMS.SubItems.Add(MyDataRow("Score2").ToString())
            LVITEMS.SubItems.Add(MyDataRow("Score3").ToString())
            LVITEMS.SubItems.Add(MyDataRow("AverageScore").ToString())
        Next i

        'display in listview
        User_listView.Items.Add(LVITEMS)
       
    End Sub

您好Max27tech,

Hi Max27tech,

此论坛讨论并询问有关Visual Studio中的测试工具的问题,包括Web性能,负载测试和编码的UI测试。

This forum is discuss and ask questions about the Test tools in Visual Studio including web performance, load testing and Coded UI testing.

由于您的问题与WinForm开发更相关,我将帮助您将此线程移至Windows窗体论坛以获得更好的帮助。

Since your problem is more related to WinForm development, I will help you move this thread to Windows Forms forum for a better help.

最诚挚的问候,

Weiwei

Best Regards,
Weiwei