繁体   English   中英

将ListView的内容(包括标题)复制到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

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

        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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM