在datagridview中显示多条记录

时间:2017-02-13 03:38:07

标签: mysql vb.net datagridview

我一直在谷歌上搜索,但还没有找到答案。它可能是我搜索的关键字。无论如何,我试图在datagridview中显示多个记录,而不是从基本的select * from table查询中显示。我是datagridviews的新手

就像在php中一样,我可以在多个div中显示不同id的多个记录,并且div会累加,直到所有记录都被提取为止。

我需要获取此数据才能进行打印。有没有办法在vb.net中执行此操作,还是应该在mysql中执行此操作?我真的不确定。可能是for eachwhile函数,但我不知道如何。

我在想这样做while loop,但我不确定:

Dim id as Integer = 0
while id < 100
    #query here
    Dim da As New MySqlDataAdapter(query, conn)
    Dim ds As New DataSet()
    If da.Fill(ds, "Monthly Time Record") Then
       DataGridView1.DataSource = ds.Tables(id)
   End If
   id += 1
End While

这是我的代码

    conn.ConnectionString = "it's a secret"
    conn.Open()
    Try
        Dim id As Integer = 0
        Dim query As String = "SELECT distinct date_format(cal.my_date,'%d') AS DAY, " +
                                "COALESCE((date_format(t.amin,'%h:%i %p')), '') AS 'AM IN', " +
                                "COALESCE((date_format(t.amout,'%h:%i %p')), '') AS 'AM OUT', " +
                                "COALESCE((date_format(t.pmin,'%h:%i %p')), '') AS 'PM IN', " +
                                "COALESCE((date_format(t.pmout,'%h:%i %p')), '') AS 'PM OUT' " +
                                "FROM " +
                                "    ( SELECT " +
                                "          s.start_date + INTERVAL (days.d) DAY  AS my_date " +
                                "      FROM " +
                                "          ( SELECT LAST_DAY(STR_TO_DATE('" & fromdate.Text & "', '%m/%d/%Y')) + INTERVAL 1 DAY - INTERVAL 1 MONTH " +
                                "                       AS start_date, " +
                                "                   LAST_DAY(STR_TO_DATE('" & fromdate.Text & "', '%m/%d/%Y'))  " +
                                "                       AS end_date " +
                                "          ) AS s " +
                                "          JOIN days  " +
                                "              ON  days.d <= DATEDIFF(s.end_date, s.start_date) " +
                                "    ) AS cal " +
                                "    LEFT JOIN attendance AS t " +
                                "        ON  t.d >= cal.my_date  " +
                                "        AND t.d  < cal.my_date + INTERVAL 1 DAY " +
                                "        and t.empID = '" & id & "'"
        Dim da As New MySqlDataAdapter(query, conn)
        Dim ds As New DataSet()

        If da.Fill(ds, "Monthly Time Record") Then
            DataGridView1.DataSource = ds.Tables(0)
            DataGridView1.ClearSelection()
        End If
        conn.Close()

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

结果是这样的(FOR A SINGLE datagridview):

Record for ID: 2
| DAY |  AM IN  |  AM OUT  |  PM IN  |  PM OUT |
|  1  | 8:00 AM | 12:00 PM | 1:00 PM | 5:00 PM |
|  2  | 8:05 AM | 12:30 PM | 1:15 PM | 5:30 PM |
|  3  | 8:00 AM | 12:15 PM | 1:05 PM | 4:45 PM |
..and so on until the end of the month

我试图获得此结果(在多个datagridviews中):

Record for ID: 2
| DAY |  AM IN  |  AM OUT  |  PM IN  |  PM OUT |
|  1  | 8:00 AM | 12:00 PM | 1:00 PM | 5:00 PM |
|  2  | 8:05 AM | 12:30 PM | 1:15 PM | 5:30 PM |
|  3  | 8:00 AM | 12:15 PM | 1:05 PM | 4:45 PM |
...and so on until the end of the month

Record for ID: 3
| DAY |  AM IN  |  AM OUT  |  PM IN  |  PM OUT |
|  1  | 9:00 AM | 12:00 PM | 1:20 PM | 5:00 PM |
|  2  | 9:05 AM | 12:00 PM | 1:15 PM | 5:30 PM |
|  3  | 8:40 AM | 12:17 PM | 1:05 PM | 4:45 PM |
...and so on until the end of the month

Record for ID: 4
| DAY |  AM IN  |  AM OUT  |  PM IN  |  PM OUT |
|  1  | 6:30 AM | 12:01 PM | 1:40 PM | 5:10 PM |
|  2  | 6:05 AM | 12:32 PM | 1:15 PM | 5:19 PM |
|  3  | 5:40 AM | 12:10 PM | 1:00 PM | 5:01 PM |
...and so on until the end of the month

0 个答案:

没有答案