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.

Recommended Answers

All 12 Replies

Perhaps use a DataGridView with one columnand with the hours in the RowHeaders?

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!

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

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?

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?

Use dt.ToString("HH:mm") instead of dt.ToString("hh:mm")

commented: oops :P Thanks +6

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

bye, bye

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.