|
ผมเขียนโค้ดเป็นตัวอย่างให้ดูเผื่อเป็นแนวทางก็ล่ะกันครับ โดยใช้ข้อมูลสมมุติแทน เวลานำไปใช้งานจริงก็ต้องดึงข้อมูลจากตาราง ซึ่งก็ขึ้นอยู่กับการออกแบบ จากภาพตัวอย่างที่ให้ผมมาดู เช่น ห้อง 305 จะมีการจองไว้ได้มากกว่า 1 วัน ผมก็เลยใช้ List(Of Integer) เข้ามาช่วย ...
*** แนะนำให้ใช้พวก Schedule Control เช่น DevExpress มาใช้งานแทนจะดีกว่าการใช้ตารางกริดครับ ***
โค้ดตัวอย่าง ... ตัดแปะโค้ดไปวางแปะไว้ในฟอร์มได้เลย เพราะผมไม่ได้ลาก DataGridView มาใส่ไว้บนฟอร์ม
- Public Class frmBooking
- '// สร้างคลาสสำหรับเก็บข้อมูลการจองห้อง
- Public Class RoomBookingInfo
- '// Status เก็บสถานะของห้อง (Available, Occupied, Booked)
- Public Property Status As String
- '// BookedDates เก็บรายการวันที่ที่ถูกจอง List(Of Integer) ตัวอย่างคือสามารถจองได้หลายวัน เช่น 10, 11, 12
- Public Property BookedDates As List(Of Integer)
- '// สถานะ และวันที่ๆระบุ
- Public Sub New(status As String, bookedDates As List(Of Integer))
- Me.Status = status
- Me.BookedDates = bookedDates
- End Sub
- End Class
- Public Sub New()
- InitializeComponent()
- LoadRoomStatus()
- End Sub
- Private Sub LoadRoomStatus()
- '// สร้าง DataGridView และเพิ่มคอลัมน์สำหรับวันที่ 1-31
- Dim dgv As New DataGridView()
- dgv.Dock = DockStyle.Fill
- Me.Controls.Add(dgv)
- '// ปรับรูปแบบของตารางกริด
- dgv.RowHeadersVisible = False
- dgv.AllowUserToAddRows = False
- dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
- '// เพิ่มคอลัมน์ "Room" และวันที่ 1-31
- dgv.Columns.Add("Room", "Room")
- For day As Integer = 1 To 31
- dgv.Columns.Add(day.ToString(), day.ToString())
- Next
- '// ข้อมูลการจองห้อง โดยใช้คลาส RoomBookingInfo (ส่วนนี้เราได้มาจากการอ่านค่าในตารางข้อมูล)
- '// RoomBookingInfo จะประกอบด้วย สถานะของห้องและวันที่ในการจองห้อง (จองได้หลายวัน)
- Dim roomBookings As New Dictionary(Of String, RoomBookingInfo) From {
- {"101", New RoomBookingInfo("Occupied", New List(Of Integer)())},
- {"102", New RoomBookingInfo("Occupied", New List(Of Integer)())},
- {"105", New RoomBookingInfo("Booked", New List(Of Integer) From {10, 11, 12})},
- {"201", New RoomBookingInfo("Available", New List(Of Integer)())},
- {"202", New RoomBookingInfo("Available", New List(Of Integer)())},
- {"203", New RoomBookingInfo("Booked", New List(Of Integer) From {20})}
- }
- '// เพิ่มแถวสำหรับแต่ละห้อง
- For Each room In roomBookings.Keys
- Dim row As New DataGridViewRow()
- row.CreateCells(dgv)
- row.Cells(0).Value = room '// ใส่หมายเลขห้องในคอลัมน์แรก
- '// ตรวจสอบสถานะของห้องและวันที่
- For day As Integer = 1 To 31
- Dim cellIndex As Integer = day '// ดัชนีของเซลล์เริ่มจาก 1 (วันที่ 1)
- Dim status As String = roomBookings(room).Status
- Dim bookedDates As List(Of Integer) = roomBookings(room).BookedDates
- '// ตรวจสอบสถานะและตั้งค่าสีพื้นหลัง
- If status = "Available" Then
- row.Cells(cellIndex).Style.BackColor = Color.White
- ElseIf status = "Occupied" Then
- row.Cells(cellIndex).Style.BackColor = Color.LightBlue
- ElseIf status = "Booked" AndAlso bookedDates.Contains(day) Then
- row.Cells(cellIndex).Style.BackColor = Color.Red
- Else
- row.Cells(cellIndex).Style.BackColor = Color.White
- End If
- Next
- '// เพิ่มแถวลงใน DataGridView
- dgv.Rows.Add(row)
- Next
- End Sub
- End Class
คัดลอกไปที่คลิปบอร์ด
|
-
|