Home > other >  C# WinForms Loop through DateTimePicker
C# WinForms Loop through DateTimePicker

Time:08-09

My application is a scheduler, build with an DataGridView.

I want to loop through a DateTimePicker and increase the days each time it loops through and add it into the DataGridView. My actual problem is, that after i click on the add button, it puts x amounts of rows into my list, but not increasing the day. So i got X lines with the same date.

How it should work: Pick a date, select the case from combobox list, interval gets added automatically. Then the actual check if the interval is (daily/weekdays). If i now click the button "add entrys", it adds the input into the datagridview. Date / Case / Interval / Added by (from userprincipal).

Here is my actual code:

        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);
                }
            
            }
        }

My next step would be to add another step for "only within the week" so it would exclude the weekend days.

EDIT: Just inserted the code from Enigmativity and it works fine!

                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;
                }

CodePudding user response:

Your problem is that datePicker.Value.AddDays(1); returns a new date. You're computing it and throwing the value away. It's as if you wrote this:

DateTime value = datePicker.Value
DateTime updated = value.AddDays(1);

You've just not done anything with updated.

You'd need this:

datePicker.Value = updated;

If it were me doing this I wouldn't rely on a control formatting your string for you. I'd separate building your rows from adding them to the grid.

Try this:

void AddInterval()
{
    if (cbInterval.Text == "daily")
    {
        var rows =
            from i in Enumerable.Range(0, 7)
            let date = datePicker.Value.AddDays(i).ToString()
            select new
            {
                date,
                caseText = cbCase.Text,
                interval = cbInterval.Text,
                userDisplay = lblUserDisplay.Text,
            };
                
        foreach (var row in rows)
        {
            schedulerGrid.Rows.Add(
                row.date,
                row.caseText,
                row.interval,
                row.userDisplay);
        }
    }
}
  • Related