c#中DataGridView中的周期时间表
Periods time schedule in DataGridView in c#
我有一个名为 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");
}
enter image description here
试试下面的代码,它从组合框中获取初始开始时间值,然后将其与今天的日期相结合以创建一个 DateTime
对象。然后通过网格行的简单循环根据“periodName”设置时间值。如果期间名称是“rest”,那么它会在日期时间上增加 5 分钟,如果期间名称不是“rest”,那么代码会在 DateTime.
上增加 40 分钟
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");
}
}
我有一个名为 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");
}
enter image description here
试试下面的代码,它从组合框中获取初始开始时间值,然后将其与今天的日期相结合以创建一个 DateTime
对象。然后通过网格行的简单循环根据“periodName”设置时间值。如果期间名称是“rest”,那么它会在日期时间上增加 5 分钟,如果期间名称不是“rest”,那么代码会在 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");
}
}