[英]C# Winforms DataGridView selected cell is clearing cell value
我正在開發一個預訂系統。 我正在使用多個 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 行設為只讀可以解決此問題,但我想保留 select 單元的功能。
通過使用 DataGridViewCellStyle.Value 設置單元格值來創建問題
bookedCell.Value = "Gravity ASD";
但我找不到任何其他方法來設置單元格值,如使用:
row.Cells[0].Value = "Class Name";
在將行添加到 DGV 之前,給我這個異常消息:指定的參數超出了有效值的范圍
如果它在該行之后添加到 DGV,它會給我這個異常消息:索引超出范圍。 必須是非負數且小於集合的大小
有沒有辦法以另一種方式將值添加到單元格,或者阻止所選單元格值被清除?
看來您可能使這比必須的更復雜。 然而; 我可能不明白要求。 經過一些小測試后,ODD 的突出之處在於代碼將行添加到網格中的方式。 從...開始…
DataGridViewRow row = new DataGridViewRow(); … ? …
這很奇怪,它可能會起作用; 但是,它是不必要的,並且正如您所描述的,它似乎會產生一些奇怪的行為。
我認為,既然您已經擁有DataGridView
... roomSchedule
和一列,那么您可能希望從“THAT GRID”中獲取“NEW ROW”,而不是從頭開始創建新行。 就像是…
int rowIndex = roomSchedule.Rows.Add();
DataGridViewRow row = roomSchedule.Rows[rowIndex];
同樣的想法也適用於行中的“CELL”。 沒有必要“創建”一個新的,因為我們從網格中得到的行已經有一個單元格。 我希望這是有道理的。
DataGridViewCell bookedCell = row.Cells[0];
這些更改似乎使網格按預期運行。 抱歉,如果我遺漏了什么,我希望這是有道理的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.