将ListView的内容(包括headers)复制到VB.net中的DataGridView

Copy the contents(including headers) of a ListView to a DataGridView in VB.net

我有一个 ListView,它的值来自 database。我用两个 MS Access OLEDB Statements 来生成我拥有的数据。所以我用了两个While Loops。第二个 OLEDB Statement 参考第一个。如何将 ListView 的所有内容插入到 DataGridView?

这是我的代码,用于调用 OLEDB Statements 的两个 While loops:

        Dim connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\STSlog.mdb;Jet OLEDB:Database Password=password;"
        Dim conn As New OleDbConnection(connectionstring)
        Dim command, command2, command3 As New OleDbCommand
        Dim commstring, commstring2, commstring3 As String
        Dim searchstring As String

        commstring = "SELECT DISTINCT empname FROM data WHERE ProjectCode = '" & PrjctCmbBox.SelectedItem.ToString & "' ORDER BY empname ASC"
        command = New OleDbCommand(commstring, conn)

        commstring3 = "SELECT SUM([Regular]) AS sRegular, SUM(OT) AS sOT FROM data WHERE ProjectCode = '" & PrjctCmbBox.SelectedItem.ToString & "' "
        command3 = New OleDbCommand(commstring3, conn)

        MainLView.Clear()
        MainLView.GridLines = True
        MainLView.FullRowSelect = True
        MainLView.View = View.Details
        MainLView.MultiSelect = True
        MainLView.Columns.Add("Employee Name", 290)
        MainLView.Columns.Add("Total Regular", 200)
        MainLView.Columns.Add("Total Overtime", 200)
        MainLView.Columns.Add("Total Hours", 200)

        conn.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        Dim RegSum, OTSum, Total As Decimal

        While reader.Read

            searchstring = reader("empname")

            commstring2 = "SELECT SUM([Regular]) AS sReg, SUM(OT) AS sOT FROM data WHERE empname = '" & searchstring & "' AND ProjectCode = '" & PrjctCmbBox.SelectedItem.ToString & "' "
            command2 = New OleDbCommand(commstring2, conn)

            Dim reader2 As OleDbDataReader = command2.ExecuteReader()

            While reader2.Read

                RegSum = (reader2("sReg"))
                OTSum = (reader2("sOT"))
                Total = Format((RegSum + OTSum), "0.0")

                With MainLView.Items.Add(reader("empname"))
                    .subitems.add(reader2("sReg"))
                    .subitems.add(reader2("sOT"))
                    .subitems.add(Total)

                End With

            End While

        End While

我先将列添加到 DataGridView,然后使用 DataGridView Rows 从数据库中插入数据。

         Dim connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\STSlog.mdb;Jet OLEDB:Database Password=password;"
        Dim conn As New OleDbConnection(connectionstring)
        Dim command, command2, command3 As New OleDbCommand
        Dim commstring, commstring2, commstring3 As String
        Dim searchstring As String

        commstring = "SELECT DISTINCT empname FROM data WHERE ProjectCode = '" & PrjctCmbBox.SelectedItem.ToString & "' ORDER BY empname ASC"
        command = New OleDbCommand(commstring, conn)

        commstring3 = "SELECT SUM([Regular]) AS sRegular, SUM(OT) AS sOT FROM data WHERE ProjectCode = '" & PrjctCmbBox.SelectedItem.ToString & "' "
        command3 = New OleDbCommand(commstring3, conn)

        MainLView.Clear()
        MainLView.GridLines = True
        MainLView.FullRowSelect = True
        MainLView.View = View.Details
        MainLView.MultiSelect = True
        MainLView.Columns.Add("Employee Name", 290)
        MainLView.Columns.Add("Total Regular", 200)
        MainLView.Columns.Add("Total Overtime", 200)
        MainLView.Columns.Add("Total Hours", 200)

        Dim col1 As New DataGridViewTextBoxColumn
        Dim col2 As New DataGridViewTextBoxColumn
        Dim col3 As New DataGridViewTextBoxColumn
        Dim col4 As New DataGridViewTextBoxColumn
        col1.HeaderText = "Employee Name"
        col2.HeaderText = "Total Regular"
        col3.HeaderText = "Total Overtime"
        col4.HeaderText = "Total Hours"
        ReportDGV.Columns.Add(col1)
        ReportDGV.Columns.Add(col2)
        ReportDGV.Columns.Add(col3)
        ReportDGV.Columns.Add(col4)

        conn.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        Dim RegSum, OTSum, Total As Decimal

        While reader.Read

            searchstring = reader("empname")

            commstring2 = "SELECT SUM([Regular]) AS sReg, SUM(OT) AS sOT FROM data WHERE empname = '" & searchstring & "' AND ProjectCode = '" & PrjctCmbBox.SelectedItem.ToString & "' "
            command2 = New OleDbCommand(commstring2, conn)

            Dim reader2 As OleDbDataReader = command2.ExecuteReader()

            While reader2.Read

                RegSum = (reader2("sReg"))
                OTSum = (reader2("sOT"))
                Total = Format((RegSum + OTSum), "0.0")

                With MainLView.Items.Add(reader("empname"))
                    .subitems.add(reader2("sReg"))
                    .subitems.add(reader2("sOT"))
                    .subitems.add(Total)

                    Dim row As String() = New String() {reader("empname"), reader2("sReg"), reader2("sOT"), Total}
                    ReportDGV.Rows.Add(row)

                End With

            End While

        End While