我有名為 TimesDgv 的 DataGridView 包含學校時期。我想自動生成時間。喜歡這個截圖影像。我怎樣才能做到這一點?
這是我的代碼
for (int i = 0; i < TimesDgv.RowCount - 1; i )
{
TimesDgv.Rows[i].Cells[1].Value = TimesDgv.Rows[i].Cells[2].Value;
DateTime time = Convert.ToDateTime(TimesDgv.Rows[i].Cells[1].Value);
TimesDgv.Rows[i].Cells[2].Value = time.AddMinutes(40).ToString("hh:mm");
}

在此處輸入圖片說明
uj5u.com熱心網友回復:
試試下面的代碼,它從組合框中獲取初始開始時間值,然后將其與今天的日期結合起來創建一個DateTime物件。然后通過網格行的簡單回圈根據“periodName”設定時間值。如果時間段名稱是“rest”,那么它會在日期時間上增加 5 分鐘,如果時間段名稱不是“rest”,那么代碼會在日期時間上增加 40 分鐘DateTime.
private void button1_Click(object sender, EventArgs e) {
string dt = DateTime.Now.ToShortDateString() " " comboBox1.Text;
if (DateTime.TryParse(dt, out DateTime currentTime)) {
for (int i = 0; i < TimesDgv.RowCount; i ) {
if (!TimesDgv.Rows[i].IsNewRow) {
if (TimesDgv.Rows[i].Cells[0].Value != null && !string.IsNullOrEmpty(TimesDgv.Rows[i].Cells[0].Value.ToString())) {
TimesDgv.Rows[i].Cells[1].Value = currentTime;
if (TimesDgv.Rows[i].Cells[0].Value.ToString() == "rest") {
currentTime = currentTime.AddMinutes(5);
}
else {
currentTime = currentTime.AddMinutes(40);
}
TimesDgv.Rows[i].Cells[2].Value = currentTime;
}
}
}
}
else {
MessageBox.Show("Invalid time in combo box");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365593.html
上一篇:如何修復應用程式退出(Winforms)時的“System.Threading.Tasks.TaskCanceledException:'Ataskwascancelled.'”例
