How can I simply do a list of hours - a date schedule (from 0 to 24) in a listView, that 1st column would show hours seperated by 30 min, 2nd column then will be filled with the name who will reserved that hour.

Something like that: PICTURE.

I would like to use listView. But even if I would use dgw, I would need the code for listing down the times (from 7.00am to 19.00pm - seperated by half an hour).

I would like to know how to do it. I did a sample for numbers:

double UraZačetka = 7.00;
            double UraNova=0;
            for (int i = 0; i < 26; i++)
            {
                if (i == 0)
                    UraNova = UraZačetka;
                else
                    UraNova = UraNova + 0.5;
                listView1.Items.Add(UraNova.ToString());
            }

Use something like this:

DateTime dt = new DateTime();
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(dt.ToShortTimeString());
                dt = dt.AddMinutes(30);
            }

EDIT: Of course you would add the shorttimestring to your listview!

Edited 6 Years Ago by ddanbe: n/a

You can also do your loop natively with a DateTime :

for (DateTime dt = DateTime.Today; dt < DateTime.Today.AddDays(1); dt = dt.AddMinutes(30))
        Console.WriteLine(dt.ToString("hh:mm"));

And how would it be if I want to start with 7.am and up to 6.pm)?
Becuase this code starts with 12.00.

Try thinking about this a little more....

private void button7_Click(object sender, EventArgs e)
    {
      for (DateTime dt = DateTime.Today.AddHours(7); dt < DateTime.Today.AddDays(1); dt = dt.AddMinutes(30))
        Console.WriteLine(dt.ToString("hh:mm"));
    }

I think you can figure out the 6pm ;)

Edited 6 Years Ago by sknake: n/a

yep :)

for (DateTime dt = DateTime.Today.AddHours(7); dt < DateTime.Today.AddHours(19); dt = dt.AddMinutes(30))
{}

thank you mate for a help.

Just one more thing, when it goes over 12.30 (in the midday) then it goes back to 1.00, I would like to have 13.00. How to do that?

Edited 6 Years Ago by Mitja Bonca: n/a

Just one more thing, when it goes over 12.30 (in the midday) then it goes back to 1.00, I would like to have 13.00. How to do that?

thx, I didnt know that until now. Big thanks.
Cheers

bye, bye

Edited 6 Years Ago by Mitja Bonca: n/a

I would like to change my code here a bit, by including dateTimePicker. That the hous in the column will be displayed from it.

Becuase I need to save into database, the time based on the dateTimePicker, not from something else (like DateTime.Today).

I did this code, but it is not starting with 7am like I would like to. It starts with 00.00.

So how would this code be changed that will include dateTimePicker?

for (DateTime Čas = DateTime.Today.AddHours(7); Čas < DateTime.Today.AddHours(19); Čas = Čas.AddMinutes(30))
            {
                dataGridView1.Rows.Add(Čas.ToShortTimeString());
                i++; 
            }

Finally did it:

DateTime čas = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day);
            
            for (DateTime Čas = čas.AddHours(7); Čas < čas.AddHours(19); Čas = Čas.AddMinutes(30))
            {
                dataGridView1.Rows.Add(Čas.ToShortTimeString());
            }

Works :)

This question has already been answered. Start a new discussion instead.