将listview中的数据存储到数组&生成随机值

时间:2014-01-26 14:11:59

标签: arrays vb.net visual-studio-2010

我非常感谢本网站提供的答案,因为它对我的学习有很大的帮助。现在的问题是,我似乎无法将listview中的第三列数据转换为数组形式。

我有一个看起来像这样的数据库,WorkerID代表Worker,TaskNo代表任务,Timetaken将代表该特定工作人员完成该特定任务所需的时间。

+----------+--------+-----------+
| WorkerID | TaskNo | TimeTaken |
+----------+--------+-----------+
| 1        | 1      | 7.5       |
+----------+--------+-----------+
| 4        | 1      | 2.5       |
+----------+--------+-----------+
| 1        | 2      | 3.5       |
+----------+--------+-----------+
| 2        | 2      | 1.5       |
+----------+--------+-----------+
| 1        | 3      | 2.5       |
+----------+--------+-----------+
| 2        | 3      | 4.5       |
+----------+--------+-----------+
| 3        | 3      | 3.5       |
+----------+--------+-----------+
| 2        | 4      | 3.5       |
+----------+--------+-----------+

- 列表继续 -

在很多帮助下,我终于可以在vb上显示它了

+--------+------------+------------------+
| TaskNo | NumWorkers | WorkersAvailable |
+--------+------------+------------------+
| 1      | 2          | 1, 4             |
+--------+------------+------------------+
| 2      | 2          | 1, 2             |
+--------+------------+------------------+
| 3      | 3          | 1, 2, 3          |
+--------+------------+------------------+
| 4      | 1          | 2                |
+--------+------------+------------------+

这是我的员工信息概述,例如有多少工人可以执行哪项任务。只有两名具有执行任务编号1的技能的员工,均为员工1&员工4.

现在我试图仅将“WorkersAvailable”中的所有值都存入并存储到数组中。

  • + ---- + ----- + ------- + - +
  • | 1,4 | 1,2 | 1,2,3 | 2 |
  • + --- + ------ + ------- + - +

通过点击一个按钮,我可以生成一串随机数,从workeravailable列中获取一个可用的随机数。

示例: 1 (第1行), 1 (第2行), 3 (第3行), 2 ( row4)......等等

注意:每项任务只需要一名工作人员

每个数字代表每行的数字,用逗号分隔。

  • 从第1行开始,我只能随机输入数字1或4
  • 从第2行开始,我只能随机选择1或2
  • 从第3行开始,我只能随机选择1或2或3

生成一个数字串(1,1,3,2)。 我必须计算完成一个任务周期所需的总时间。每个周期代表一个接一个地完成所有必需的任务。现在我必须检查数据库的不同时间,将它们加在一起以获得一个周期的总时间。

我的代码

   Sub RefreshLv()

        Using conn = New OleDbConnection
            conn.ConnectionString = myConString
            conn.Open()

            Dim dt As New System.Data.DataTable("ListInfo")
            Using da

                ' fill the DataTable with three columns, the third column being a placeholder that we will fill in below
                Dim Sql As String = ("SELECT [TaskNo], COUNT(*) AS NumWorkers, '' AS WorkersAvailable " & "FROM ScheduleInfo GROUP BY [TaskNo]")
                da.SelectCommand = New OleDbCommand(Sql, conn)
                da.Fill(dt)

                Using cmd2 = New OleDbCommand()

                    ' create a Prepared Statement that we will use for each iteration
                    cmd2.Connection = conn
                    cmd2.CommandText = "SELECT [WorkerID] FROM ScheduleInfo " & "WHERE [TaskNo] = ? " & "ORDER BY [WorkerID]"
                    cmd2.Parameters.Add("?", OleDbType.Integer)
                    cmd2.Prepare()

                    LstViewScheduleInfo.Columns.Add("TaskNo", 150, HorizontalAlignment.Center)
                    LstViewScheduleInfo.Columns.Add("No of Workers", 150, HorizontalAlignment.Center)
                    LstViewScheduleInfo.Columns.Add("Workers Available", 150, HorizontalAlignment.Center)

                    ' foreach row of the DataTable, build the string of WorkerID values
                    Dim RandomEmployee As New List(Of Integer)
                    Dim rnd As New Random

                    For Each dr As System.Data.DataRow In dt.Rows

                        Dim ListEmployee As New List(Of Integer)
                        Dim workerList As String = ""
                        cmd2.Parameters(0).Value = dr("TaskNo")
                        Using rdr As OleDbDataReader = cmd2.ExecuteReader()

                            While rdr.Read()
                                ListEmployee.Add(rdr("WorkerID"))
                                workerList += ", " & rdr("WorkerID")
                            End While
                        End Using

                        ' remove leading ", "
                        'Dim Qpass As String
                        dr("WorkersAvailable") = workerList.Substring(2)
                        Dim randomvalue As Integer = ListEmployee(rnd.Next(0, 2))
                        'Qpass = randomvalue.ToString()
                        'MessageBox.Show(Qpass)
                        RandomEmployee.Add(randomvalue)
                    Next
                    Dim zxc = String.Join((","), RandomEmployee.ToArray())
                    LblRandom1.Text = zxc

                End Using

                ' for demo purposes, just dump the DataTable to the console
                For Each dr As DataRow In dt.Rows()
                    Dim lst As ListViewItem
                    lst = LstViewScheduleInfo.Items.Add(dr(0))

                    For i As Integer = 1 To dt.Columns.Count - 1
                        lst.SubItems.Add(dr(i))
                    Next
                Next

            End Using
            conn.Close()
        End Using


    End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        RefreshLv()

 End Sub
    Private Sub BtnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRandom.Click

        LstViewScheduleInfo.Clear()
        RefreshLv()

    End Sub
End Class

我试图在每次程序循环检查Workeravailable时尝试获取List(整数)。然后选择一个随机数并使用消息框显示随机数。我知道这不对,但我真的不知道在我的程序中要求随机值。 这是我第一次接触阵列,所以任何帮助将不胜感激。

0 个答案:

没有答案