You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms:DateTimePicker循环日期递增写入DataGridView问题

问题:DataGridView调度器日期未递增的问题

我开发了一款基于DataGridView的调度器应用程序,需求是循环遍历DateTimePicker,每次循环递增日期并将数据写入DataGridView。目前遇到的问题是:点击添加按钮后,虽能生成指定行数,但所有行的日期均相同,未实现日期递增。

预期流程

  • 选择日期,从ComboBox列表中选择案例,自动添加间隔
  • 判断间隔类型(每日/工作日)
  • 点击“添加条目”按钮后,将日期、案例、间隔、添加人(来自UserPrincipal)写入DataGridView

现有代码

void Button1Click(object sender, EventArgs e) // Add intervall
{
    addIntervall();
}
void addIntervall()
{           
    if (cbIntervall.Text == "daily")
    {
        for (int i = 0; i < 7; i++)
        {
            datePicker.Value.AddDays(1);
            schedulerGrid.Rows.Add(datePicker.Text, cbCase.Text, cbIntervall.Text, lblUserDisplay.Text);
        }
    }
}

下一步计划

添加“仅工作日”逻辑,排除周末日期。

编辑补充(问题已解决)

采用Enigmativity提供的代码修复了问题,修正后代码如下:

for (int i = 0; i < 7; i++)
{
    DateTime value = datePicker.Value;
    DateTime updated = value.AddDays(1);
    schedulerGrid.Rows.Add(datePickerControl.Text, datePicker.Text, cbCase.Text, cbIntervall.Text, lblUserDisplay.Text);
    datePicker.Value = updated;
}

内容的提问来源于stack exchange,提问作者NutritiousTea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 19:18:19