我正在開發一個預訂系統。我使用多個 DGV 根據 DGV 中的 30 分鐘插槽單元直觀地顯示不同房間的預訂插槽,每個房間都有自己的 DGV。我在每個房間上使用了 foreach 陳述句來創建一個新的 DGV,適當地更改屬性,為每個時間段添加行,如果它們與預訂時間匹配,則在將其添加到 DGV 之前更改單元格顏色和值并將 DGV 添加到表格。
代碼如下:
public partial class BookingSystem : Form
{
List<string> RoomList = new List<string>();
public BookingSystem()
{
InitializeComponent();
this.Text = "Booking System - " DateTime.Today.DayOfWeek.ToString() " " DateTime.Today.ToString("dd MMM yyyy");
RoomList.Add("Community Room 3");
RoomList.Add("Community Room 2");
RoomList.Add("Community Room 1");
RoomList.Add("Sports Hall");
//RoomList.Add("New Room");
DateTime bookingStart = DateTime.Parse("23/11/2021 10:30:00");
DateTime bookingStart2 = DateTime.Parse("23/11/2021 11:00:00");
DateTime bookingEnd = DateTime.Parse("23/11/2021 11:30:00");
this.Width = 1080;
foreach (string room in RoomList)
{
DataGridViewCellStyle dgvCellStyle = new DataGridViewCellStyle();
Color color = Color.FromArgb(255, 224, 224, 224);
dgvCellStyle.BackColor = color;
DataGridView roomSchedule = new DataGridView();
roomSchedule.Dock = DockStyle.Left;
roomSchedule.Width = 200;
roomSchedule.Columns.Add(room, room);
roomSchedule.Columns[0].Width = 135;
roomSchedule.RowHeadersWidth = 63;
roomSchedule.AlternatingRowsDefaultCellStyle = dgvCellStyle;
roomSchedule.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
roomSchedule.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
//roomSchedule.ReadOnly = true;
roomSchedule.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
roomSchedule.AllowUserToResizeRows = false;
DateTime openingTime = DateTime.Parse("07:00");
DateTime closingTime = DateTime.Parse("22:00");
TimeSpan openingHours = closingTime - openingTime;
DateTime currentTimeSlot = openingTime;
double totalRows = openingHours.TotalMinutes / 30;
int rows = 0;
while (rows <= totalRows)
{
DataGridViewRow row = new DataGridViewRow();
DataGridViewCellStyle bookedCellStyle = new DataGridViewCellStyle();
bookedCellStyle.BackColor = Color.Moccasin;
DataGridViewCell bookedCell = new DataGridViewTextBoxCell();
bookedCell.Value = "Class Name";
bookedCell.Style = bookedCellStyle;
row.HeaderCell.Value = currentTimeSlot.ToString("HH:mm");
roomSchedule.Rows.Add(row);
if (currentTimeSlot.TimeOfDay == bookingStart.TimeOfDay || currentTimeSlot.TimeOfDay == bookingStart2.TimeOfDay)
{
if (room == "Sports Hall")
{
row.Cells[0] = bookedCell;
//row.Cells[0].Value = "Class Name";
}
}
currentTimeSlot = currentTimeSlot.AddMinutes(30);
roomSchedule.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
rows ;
}
dgvBox.Controls.Add(roomSchedule);
}
}
}
但是,當以這種方式輸入單元格的值時,只要選擇了該單元格,該值就會被清除。
禁用 DGV 或將 DGV 行設為只讀可以解決此問題,但我希望保留選擇單元格的能力。
問題是通過使用 DataGridViewCellStyle.Value 設定單元格值來創建的
bookedCell.Value = "Gravity ASD";
但我找不到任何其他方法來設定單元格值,如使用:
row.Cells[0].Value = "Class Name";
在將行添加到 DGV 之前,給我這個例外訊息:指定的引數超出了有效值的范圍
如果它在行添加到 DGV 之后,它會給我這個例外訊息:索引超出范圍。必須是非負的并且小于集合的大小
有沒有辦法以另一種方式將值添加到單元格或阻止所選單元格值被清除?
uj5u.com熱心網友回復:
看起來您可能使這變得比它必須的更復雜。然而; 我可能不明白這些要求。經過一些小測驗后,最突出的是 ODD 是代碼將行添加到網格的方式。從...開始…
DataGridViewRow row = new DataGridViewRow(); … ? …
這很奇怪,它可能會起作用;但是,這是不必要的,而且正如您所描述的那樣,它似乎會產生一些奇怪的行為。
我認為,既然你已經有了DataGridView... roomScheduleWITH 一列,你可能想從“那個網格”中獲得一個“新行”,而不是從頭開始創建一個新行。就像是…
int rowIndex = roomSchedule.Rows.Add();
DataGridViewRow row = roomSchedule.Rows[rowIndex];
同樣的想法也適用于該行中的“CELL”。沒有必要“創建”一個新的,因為我們從網格中得到的行已經有一個單元格。我希望這是有道理的。
DataGridViewCell bookedCell = row.Cells[0];
這些更改似乎使網格按預期運行。對不起,如果我遺漏了什么,我希望這是有道理的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365586.html
