473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Code to add times to dropdownlist

I have a dropdownlist. I want to fill it with times (using vb.net code).

I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
incrementing every half hour.

I know I could type in the values manually but something tells me that I
could loop thru a time variable and add 1/2 hr, etc... then add it in a
loop. (More efficient.)

Any coders want to give this a shot? The less code the better. Thanks!
Jul 22 '08 #1
7 8172


myDDL.Items.Add(new ListItem ("Something1", "Something2") );

You can write a loop to go through the items.

for(int i = 8 ; i <= 17 ; i ++ )
{
for (int j = 0 ; j < 2 ; j ++ )
{

//now you have
//8 and 0 ( "8:00" )
//8 and 1 (" 8:30")
string hourAndMinute = Convert.ToString(i);
if(j == 0)
{
hourAndMinute += ":00";

}
else
{
hourAndMinute +=":30";
}

myDDL.Items.Add(new ListItem ( hourAndMinute , hourAndMinute ) );
}

}
I'll leave it to you for the AM PM. And the convert (from) military time
hours.

But that should get you started.
"Cirene" <ci****@nowhere.comwrote in message
news:e2**************@TK2MSFTNGP04.phx.gbl...
>I have a dropdownlist. I want to fill it with times (using vb.net code).

I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
incrementing every half hour.

I know I could type in the values manually but something tells me that I
could loop thru a time variable and add 1/2 hr, etc... then add it in a
loop. (More efficient.)

Any coders want to give this a shot? The less code the better. Thanks!

Jul 22 '08 #2
Thanks. So that I do not have to do too much conversion I was wondering if
there was a way to have a time variable of 8:00 AM and just add 30 min to it
over and over again until you reach 5:30 PM. Hmmm...

"sloan" <sl***@ipass.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>

myDDL.Items.Add(new ListItem ("Something1", "Something2") );

You can write a loop to go through the items.

for(int i = 8 ; i <= 17 ; i ++ )
{
for (int j = 0 ; j < 2 ; j ++ )
{

//now you have
//8 and 0 ( "8:00" )
//8 and 1 (" 8:30")
string hourAndMinute = Convert.ToString(i);
if(j == 0)
{
hourAndMinute += ":00";

}
else
{
hourAndMinute +=":30";
}

myDDL.Items.Add(new ListItem ( hourAndMinute , hourAndMinute ) );
}

}
I'll leave it to you for the AM PM. And the convert (from) military time
hours.

But that should get you started.
"Cirene" <ci****@nowhere.comwrote in message
news:e2**************@TK2MSFTNGP04.phx.gbl...
>>I have a dropdownlist. I want to fill it with times (using vb.net code).

I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
incrementing every half hour.

I know I could type in the values manually but something tells me that I
could loop thru a time variable and add 1/2 hr, etc... then add it in a
loop. (More efficient.)

Any coders want to give this a shot? The less code the better. Thanks!


Jul 22 '08 #3
"Cirene" <ci****@nowhere.comwrote in message
news:ew****************@TK2MSFTNGP06.phx.gbl...
Thanks. So that I do not have to do too much conversion I was wondering
if there was a way to have a time variable of 8:00 AM and just add 30 min
to it over and over again until you reach 5:30 PM. Hmmm...
Absolutely!

1) Declare the starting DateTime variable

2) Set up a while (...) loop

3) Use the .AddMinutes(30) variable to increment the DateTime variable

4) Add a new ListItem each time through the loop.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 22 '08 #4

Right ohh.

But if you knew that (OP), then just let 'er fly (code it up) !

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Cirene" <ci****@nowhere.comwrote in message
news:ew****************@TK2MSFTNGP06.phx.gbl...
>Thanks. So that I do not have to do too much conversion I was wondering
if there was a way to have a time variable of 8:00 AM and just add 30 min
to it over and over again until you reach 5:30 PM. Hmmm...

Absolutely!

1) Declare the starting DateTime variable

2) Set up a while (...) loop

3) Use the .AddMinutes(30) variable to increment the DateTime variable

4) Add a new ListItem each time through the loop.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 22 '08 #5
Here is a sample that does what you need, and is highly flexible.

First the page itself (test page) - there are two drop downs here, as the
code is set up to setup as many time dropdowns as you need.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>

Now the .cs file

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl = this.DropDownList1;
LoadTimeDropDownList(ref ddl, 8, 17, 30);

ddl = this.DropDownList2;
LoadTimeDropDownList(ref ddl, 6, 22, 60);
}

private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);

LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}

private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;

while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);

if ((startTime now) && (!haveIndex))
{
ddl.SelectedIndex = ddl.Items.Count - 1;
haveIndex = true;
}
}

}

}

This is a down and dirty try at this, but you should see two different drop
downs, with different parameters. You can encapsulate this in a user control
and use it over and over again, by creating parameters for start hour, end
hour and increment.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"Cirene" <ci****@nowhere.comwrote in message
news:e2**************@TK2MSFTNGP04.phx.gbl...
>I have a dropdownlist. I want to fill it with times (using vb.net code).

I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
incrementing every half hour.

I know I could type in the values manually but something tells me that I
could loop thru a time variable and add 1/2 hr, etc... then add it in a
loop. (More efficient.)

Any coders want to give this a shot? The less code the better. Thanks!
Jul 22 '08 #6
I already posted the complete code, but here is the portions with the loop:

private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);

LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}

private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;

while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);

if ((startTime now) && (!haveIndex))
{
ddl.SelectedIndex = ddl.Items.Count - 1;
haveIndex = true;
}
}
}
The overload makes it so you can pass in the date time objects, or not,
depending on your own personal ideas. The DropDownList is added by ref here
so you can have many dropdowns with different time parameters.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"Cirene" <ci****@nowhere.comwrote in message
news:ew****************@TK2MSFTNGP06.phx.gbl...
Thanks. So that I do not have to do too much conversion I was wondering
if there was a way to have a time variable of 8:00 AM and just add 30 min
to it over and over again until you reach 5:30 PM. Hmmm...

"sloan" <sl***@ipass.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>

myDDL.Items.Add(new ListItem ("Something1", "Something2") );

You can write a loop to go through the items.

for(int i = 8 ; i <= 17 ; i ++ )
{
for (int j = 0 ; j < 2 ; j ++ )
{

//now you have
//8 and 0 ( "8:00" )
//8 and 1 (" 8:30")
string hourAndMinute = Convert.ToString(i);
if(j == 0)
{
hourAndMinute += ":00";

}
else
{
hourAndMinute +=":30";
}

myDDL.Items.Add(new ListItem ( hourAndMinute , hourAndMinute ) );
}

}
I'll leave it to you for the AM PM. And the convert (from) military time
hours.

But that should get you started.
"Cirene" <ci****@nowhere.comwrote in message
news:e2**************@TK2MSFTNGP04.phx.gbl...
>>>I have a dropdownlist. I want to fill it with times (using vb.net code).

I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
incrementing every half hour.

I know I could type in the values manually but something tells me that I
could loop thru a time variable and add 1/2 hr, etc... then add it in a
loop. (More efficient.)

Any coders want to give this a shot? The less code the better. Thanks!


Jul 22 '08 #7
Wow - u guys are awesome - thanks!

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
Here is a sample that does what you need, and is highly flexible.

First the page itself (test page) - there are two drop downs here, as the
code is set up to setup as many time dropdowns as you need.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>

Now the .cs file

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl = this.DropDownList1;
LoadTimeDropDownList(ref ddl, 8, 17, 30);

ddl = this.DropDownList2;
LoadTimeDropDownList(ref ddl, 6, 22, 60);
}

private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);

LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}

private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;

while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);

if ((startTime now) && (!haveIndex))
{
ddl.SelectedIndex = ddl.Items.Count - 1;
haveIndex = true;
}
}

}

}

This is a down and dirty try at this, but you should see two different
drop downs, with different parameters. You can encapsulate this in a user
control and use it over and over again, by creating parameters for start
hour, end hour and increment.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"Cirene" <ci****@nowhere.comwrote in message
news:e2**************@TK2MSFTNGP04.phx.gbl...
>>I have a dropdownlist. I want to fill it with times (using vb.net code).

I want the entries in the ddl to be from "8:00 AM" to "5:00 PM",
incrementing every half hour.

I know I could type in the values manually but something tells me that I
could loop thru a time variable and add 1/2 hr, etc... then add it in a
loop. (More efficient.)

Any coders want to give this a shot? The less code the better. Thanks!

Jul 22 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
by: Kalyani | last post by:
Hi, I have a page with a DropDownList populated with some values.On a button click one of the value in that DropDownList DataSource is removed.But to update the DropDownList contents the page must...
3
by: Northern | last post by:
I need to write code clear the display box of my DropDownList (something like clear current selected item) while still keep the loaded item list in the DropDownList. I tried DropDownList's...
9
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
15
by: Swetha | last post by:
Hello I have a DropDownList that I am populating using the following SqlDataSource: <asp:DropDownList ID="parentIDDropDownList" runat="server" DataSourceID="SqlDataSource3"...
0
by: graphicsxp | last post by:
Hi, WebPage Description: Contains one datagrid with link button column and one gridview with dropdownlist column. <asp:LinkButton id="QueryName" runat="server" autopostback="true"...
3
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView...
2
by: rajendrsedhain | last post by:
Hi, I have 1 checkboxlist, 5 dropdownlits and three textboxes.I have to write the SQL query and c# code for that advanced search. <asp:CheckBoxList ID="reposotoryCheckBoxList" runat="server"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.