Solved

# How can I create dynamic quarter values in a dropdown list?

Posted on 2011-05-13
1,123 Views
I have a need to dynamically create quarter values on the fly in a dropdown list. I need an item/value for Jan 10 2011, Apr 10 2011, Jul 10 2011 and Oct 10 2011 for the current and past three years to always be visible in the dropdown for a reporting application. Please see the example below...

Jan 10, 2011
April 10, 2011
Jul 10, 2011
Oct 10, 2011
Jan 10, 2010
April 10, 2010
Jul 10, 2010
Oct 10, 2010
Jan 10, 2009
April 10, 2009
Jul 10, 2009
Oct 10, 2009
Jan 10, 2008
April 10, 2008
Jul 10, 2008
Oct 10, 2008
0
Question by:devnewbee

LVL 13

Expert Comment

I would do something similar to below
``````Dim YearsBack As Integer = 3
For I = 0 To YearsBack
Dim Q4 As New Date(Now.Year - I, 10, 10)
Dim Q3 As New Date(Now.Year - I, 7, 10)
Dim Q2 As New Date(Now.Year - I, 4, 10)
Dim Q1 As New Date(Now.Year - I, 1, 10)
'The order they are added could be adjusted any way you want
Next
``````
0

Author Comment

Corey2:

I tried the below in C#. The only thing that lists in the dd is Q1,Q2,Q3,Q4 (repeating)..

int YearsBack = 3;
int I = 0;
DateTime Now = System.DateTime.Now;
for (I = 0; I <= YearsBack; I++)
{
System.DateTime Q4 = new System.DateTime(Now.Year - I, 10, 10);
System.DateTime Q3 = new System.DateTime(Now.Year - I, 7, 10);
System.DateTime Q2 = new System.DateTime(Now.Year - I, 4, 10);
System.DateTime Q1 = new System.DateTime(Now.Year - I, 1, 10);

}
0

LVL 16

Assisted Solution

Hi,
you can us following logic, I am using List of DateTime but you can use list of string or any structure as per requirements

public List<DateTime> GetQuater(DateTime startDate, int years)
{
List<DateTime> lst = new List<DateTime>();
while (startDate < endDate)
{
}
return lst;
}
Thanks
0

LVL 13

Expert Comment

``````int YearsBack = 3;
int I = 0;
DateTime Now = System.DateTime.Now;
for (I = 0; I <= YearsBack; I++)
{
System.DateTime Q4 = new System.DateTime(Now.Year - I, 10, 10);
System.DateTime Q3 = new System.DateTime(Now.Year - I, 7, 10);
System.DateTime Q2 = new System.DateTime(Now.Year - I, 4, 10);
System.DateTime Q1 = new System.DateTime(Now.Year - I, 1, 10);

}
``````
0

LVL 16

Expert Comment

and you can bind that

DropDownList3.DataSource = this.GetQuater(new DateTime(2008, Base64FormattingOptions, 10), 3);
DropDownList3.DataBind();

if you are using other approach then use follwoing after removing double qoutaion marks "

0

Author Comment

VS still does not like it. It says Error      2      Argument 1: cannot convert from 'System.DateTime' to 'System.Web.UI.WebControls.ListItem'
0

Author Comment

IJZ, I used the following and VS is unhappy as well.
protected void Page_Load(object sender, EventArgs e)
{
DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, Base64FormattingOptions, 10), 3);
DropDownList3.DataBind();
}
}
public List<DateTime> GetQuarter(DateTime startDate, int years)
{
List<DateTime> lst = new List<DateTime>();
while (startDate < endDate)
{
}
return lst;
}
0

LVL 16

Expert Comment

It will be better to use my code but following will resolve your problem too
0

LVL 16

Expert Comment

it is
DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);

Base64FormattingOptions is typed by error

0

LVL 13

Assisted Solution

Q1.ToString() may work otherwise you will want to use Format to get the desired look
you can also pass a format to the tostring method
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
0

Author Comment

IJZ, I used the GetQuarter stub and it works well. However, I am unable to bind with what you sent. VS is unhappy with

DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, Base64FormattingOptions, 10), 3);
0

LVL 16

Expert Comment

it is
DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);

Base64FormattingOptions is typed by error

0

Author Comment

OK, partial success. When I use the following in page_load:

DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);
DropDownList3.DataBind();

Is public List<DateTime> GetQuarter(DateTime startDate, int years) missing something.

It says GetQuarter has no extension method.
0

LVL 13

Expert Comment

IJZ,

Your solution doesn't allow the Author to get the list in the order he outlined in his OP, or am I missing something?
0

Author Comment

Corey, I got your solution to work with:

int YearsBack = 3;
int I = 0;
DateTime Now = System.DateTime.Now;
for (I = 0; I <= YearsBack; I++)
{
System.DateTime Q4 = new System.DateTime(Now.Year - I, 10, 10);
System.DateTime Q3 = new System.DateTime(Now.Year - I, 7, 10);
System.DateTime Q2 = new System.DateTime(Now.Year - I, 4, 10);
System.DateTime Q1 = new System.DateTime(Now.Year - I, 1, 10);

}
0

Accepted Solution

IJZ, I got your solution to work with:

public List<DateTime> GetQuarter(DateTime startDate, int years)
{
List<DateTime> lst = new List<DateTime>();
while (startDate < endDate)
{
}
return lst;
}
0

LVL 16

Expert Comment

That's nice
0

Author Closing Comment

Huge thanks to you both. I got this task done and learned at the same time. I appreciate your assistance very much.
0

## Featured Post

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
how to add IIS SMTP to handle application/Scanner relays into office 365.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…