如何将两个日期之间的数据绑定到DataGridView中

时间:2016-11-20 08:11:27

标签: mysql vb.net data-binding datagridview

如何将两个日期之间的数据绑定到VB.NET中的DataGridView

我有3个表mysql

TblRoom

 | KdRoom | RoomName         |
 | 1001   | Room 1 1st Floor |
 | 1002   | Room 2 1st Floor |
 | 2001   | Room 1 2nd Floor |
 | 2002   | Room 2 2nd Floor |

TblBookingMain

 | KdBooking | GuestName |
 | BOOK1     | Mr. A     |
 | BOOK2     | Mr. B     |
 | BOOK3     | Mr. C     |

TblBookingSub

 | KdBooking | KdRoom | CIDate              | CODate              | Status |
 | BOOK1     | 1001   | 2016-11-18 14:00:00 | 2016-11-19 12:00:00 | Booked |
 | BOOK1     | 1001   | 2016-11-19 14:00:00 | 2016-11-20 12:00:00 | Booked |
 | BOOK2     | 1002   | 2016-11-19 14:00:00 | 2016-11-20 12:00:00 | Booked |
 | BOOK2     | 1002   | 2016-11-20 14:00:00 | 2016-11-21 12:00:00 | Booked |
 | BOOK2     | 1002   | 2016-11-21 14:00:00 | 2016-11-22 12:00:00 | Booked |
 | BOOK3     | 1001   | 2016-11-20 14:00:00 | 2016-11-21 12:00:00 | Booked |
 | BOOK3     | 1001   | 2016-11-21 14:00:00 | 2016-11-22 12:00:00 | Booked |
 | BOOK3     | 1001   | 2016-11-22 14:00:00 | 2016-11-23 12:00:00 | Booked |

这是我的vb.net代码:

Sub LoadFormatgvPanelRoomRack()

gvPanelRoomRack.Columns.Clear()
gvPanelRoomRack.Rows.Clear()

Dim pTimeCI As DateTime
Dim pTimeCO As DateTime

Try
con.Close()
con.Open()
strSql = "SELECT tp_tblsttimerangesetting.KdTRS," & _
                "tp_tblsttimerangesetting.CITime," & _
                "tp_tblsttimerangesetting.COTime," & _
                "tp_tblsttimerangesetting.KdActiveStatus" & _
         " FROM tp_tblsttimerangesetting" & _
         " WHERE tp_tblsttimerangesetting.KdActiveStatus=1"
cmd = New MySqlCommand(strSql, con)
dr = cmd.ExecuteReader
dr.Read()
    If dr.HasRows Then
        pTimeCI = dr.Item(1).ToString
        pTimeCO = dr.Item(2).ToString
    End If
dr.Close()
dr.Dispose()
Catch ex As Exception
    Beep()
    MsgBox("Error LoadDataOccupancy: " & ex.Message)
Finally
    con.Close()
    con.Dispose()
End Try

pTimeCI = pTimeCI.ToLongTimeString
pTimeCO = pTimeCO.ToLongTimeString

Dim pDateCI As Date
Dim pDateCO As Date

If dtpStartRoomRack.Text = "" Then
    pDateCI = Now
Else
    pDateCI = dtpStartRoomRack.Text
End If
If dtpEndRoomRack.Text = "" Then
    pDateCO = Now
Else
    pDateCO = dtpEndRoomRack.Text
End If

With gvPanelRoomRack
    .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
    .ColumnHeadersHeight = 40
    .RowHeadersWidth = 41

    Dim colRoomNumber As New DataGridViewButtonColumn()
    With colRoomNumber
        .Name = "RoomNumber"
        .HeaderText = "No."
        .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .SortMode = DataGridViewColumnSortMode.NotSortable
        .FlatStyle = FlatStyle.Standard
    End With
    .Columns.Add(colRoomNumber)

    Dim colRoomType As New DataGridViewButtonColumn()
    With colRoomType
        .Name = "RoomType"
        .HeaderText = "R. Type"
        .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .SortMode = DataGridViewColumnSortMode.NotSortable
        .FlatStyle = FlatStyle.Standard
     End With
     .Columns.Add(colRoomType)

    Dim colBedType As New DataGridViewButtonColumn()
    With colBedType
        .Name = "BedType"
        .HeaderText = "Bed"
        .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .SortMode = DataGridViewColumnSortMode.NotSortable
        .FlatStyle = FlatStyle.Standard
    End With
    .Columns.Add(colBedType)

    Dim colToday As New DataGridViewButtonColumn()
    With colToday
        .Name = "Today"
        .HeaderText = "Today"
        .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .SortMode = DataGridViewColumnSortMode.NotSortable
        .FlatStyle = FlatStyle.Standard
    End With
    .Columns.Add(colToday)

    Dim DayCount As Integer = DateDiff(DateInterval.Day, pDateCI, pDateCO)
    For i = 0 To DayCount - 1
        .Columns.Add(i, (Format(pDateCI, "yyyy/MM/dd") & " " & pTimeCI))
        .Columns.Add(i, (Format(pDateCI.AddDays(1), "yyyy/MM/dd") & " " & pTimeCO))
        pDateCI = pDateCI.AddDays(1)
    Next

    .GridColor = Color.Blue
    .DefaultCellStyle.ForeColor = Color.Black
    .RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
    .AlternatingRowsDefaultCellStyle.BackColor = Color.White
    .GridColor = Color.Silver

End With

End Sub


Sub LoadDatagvPanelRoomRack()

strSql = "SELECT tp_tblmsroom.KdRoom," & _
                "tp_tblmsroom.NmRoom," & _
                "tp_tblmsroom.KdRoomType," & _
                "tp_tblmsroomtype.NmRoomType," & _
                "tp_tblmsroomtype.KdBedType," & _
                "tp_tblmsbedtype.NmBedType," & _
                "tp_tblmsroom.KdRoomStatus" & _
         " FROM tp_tblmsroomtype" & _
                " RIGHT OUTER JOIN tp_tblmsroom ON (tp_tblmsroomtype.KdRoomType = tp_tblmsroom.KdRoomType)" & _
                " LEFT OUTER JOIN tp_tblmsbedtype ON (tp_tblmsroomtype.KdBedType = tp_tblmsbedtype.KdBedType)" & _
         " ORDER BY tp_tblmsroom.KdRoom ASC"

Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim dt As New DataTable
Dim i As Integer
Try
    con.Close()
    con.Open()
    With cmd
        .CommandText = strSql
        .Connection = con
    End With
    With da
        .SelectCommand = cmd
        .Fill(dt)
    End With

    gvPanelRoomRack.Rows.Clear()

    For i = 0 To dt.Rows.Count - 1
    With gvPanelRoomRack
        .Rows.Add(dt.Rows(i)("KdRoom"),
                  dt.Rows(i)("KdRoomType"),
                  dt.Rows(i)("KdBedType"),
                  dt.Rows(i)("KdRoomStatus"))
    End With
    Next
Catch ex As Exception
    MsgBox(ex.Message)
End Try

cmd.Dispose()
da.Dispose()
con.Close()

End Sub  

Private Sub dtpStartRoomRack_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartRoomRack.ValueChanged
    LoadFormatgvPanelRoomRack()
    LoadDatagvPanelRoomRack()
End Sub

Private Sub dtpEndRoomRack_ValueChanged(sender As Object, e As EventArgs) Handles dtpEndRoomRack.ValueChanged
    LoadFormatgvPanelRoomRack()
    LoadDatagvPanelRoomRack()
End Sub  

我尝试更改mysql代码:

SELECT tp_tblmsroom.KdRoom,
       tp_tblmsroom.NmRoom,
       tp_tblmsroom.KdRoomType,
       tp_tblmsroomtype.NmRoomType,
       tp_tblmsroomtype.KdBedType,
       tp_tblmsbedtype.NmBedType,
       tp_tblmsroom.KdRoomStatus
FROM tp_tblmsroomtype
       RIGHT OUTER JOIN tp_tblmsroom ON (tp_tblmsroomtype.KdRoomType = tp_tblmsroom.KdRoomType)
       LEFT OUTER JOIN tp_tblmsbedtype ON (tp_tblmsroomtype.KdBedType = tp_tblmsbedtype.KdBedType)
ORDER BY tp_tblmsroom.KdRoom ASC  

要:

SELECT tp_tblmsroom.KdRoom,
        COALESCE(MAX(DISTINCT CASE WHEN tp_tbltsbookingsubsecondary.TglCheckIn = '" & dtpStartRoomRack.value & "' THEN tp_tbltsbookingmain.NmPelanggan END),'') AS '" & dtpStartRoomRack.value & "'
FROM tp_tbltsbookingmain
        RIGHT OUTER JOIN tp_tbltsbookingsubsecondary ON (tp_tbltsbookingmain.KdBooking = tp_tbltsbookingsubsecondary.KdBooking)
        RIGHT OUTER JOIN tp_tblmsroom ON (tp_tbltsbookingsubsecondary.KdRoom = tp_tblmsroom.KdRoom)
GROUP BY tp_tblmsroom.KdRoom 

但按日期输出只运行一个,我需要在dtpStartRoomRack.valuedtpEndRoomRack.value之间动态循环。

这是我的vb.net节目:

Room Availability

什么mysql和vb.net代码适合我的情况?

0 个答案:

没有答案