• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1327
  • Last Modified:

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

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
devnewbee
Asked:
devnewbee
  • 8
  • 6
  • 4
3 Solutions
 
Corey ScheichDeveloperCommented:
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
                Listbox1.add(Q1)
                Listbox1.add(Q2)
                Listbox1.add(Q3)
                Listbox1.add(Q4)
            Next

Open in new window

0
 
devnewbeeAuthor Commented:
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);

                DropDownList3.Items.Add("Q1");
                DropDownList3.Items.Add("Q2");
                DropDownList3.Items.Add("Q3");
                DropDownList3.Items.Add("Q4");
               
            }
0
 
Imran Javed ZiaCommented:
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>();
            DateTime endDate = startDate.AddYears(years);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }
Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Corey ScheichDeveloperCommented:
By adding quotes around them you made them into literal strings
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);

                DropDownList3.Items.Add(Q1);
                DropDownList3.Items.Add(Q2);
                DropDownList3.Items.Add(Q3);
                DropDownList3.Items.Add(Q4);
               
            }

Open in new window

0
 
Imran Javed ZiaCommented:
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 "
 DropDownList3.Items.Add(Q1);
                DropDownList3.Items.Add(Q2);
                DropDownList3.Items.Add(Q3);
                DropDownList3.Items.Add(Q4);

0
 
devnewbeeAuthor Commented:
I used     DropDownList3.Items.Add(Q1);
                DropDownList3.Items.Add(Q2);
                DropDownList3.Items.Add(Q3);
                DropDownList3.Items.Add(Q4);
               
VS still does not like it. It says Error      2      Argument 1: cannot convert from 'System.DateTime' to 'System.Web.UI.WebControls.ListItem'
0
 
devnewbeeAuthor Commented:
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>();
            DateTime endDate = startDate.AddYears(years);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }
0
 
Imran Javed ZiaCommented:
It will be better to use my code but following will resolve your problem too
DropDownList3.Items.Add(Q1.ToString());
0
 
Imran Javed ZiaCommented:
it is
DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);


Base64FormattingOptions is typed by error

0
 
Corey ScheichDeveloperCommented:
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
 
devnewbeeAuthor Commented:
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
 
Imran Javed ZiaCommented:
it is
DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);

Base64FormattingOptions is typed by error

0
 
devnewbeeAuthor Commented:
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
 
Corey ScheichDeveloperCommented:
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
 
devnewbeeAuthor Commented:
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);

                DropDownList3.Items.Add(Q1.ToShortDateString());
                DropDownList3.Items.Add(Q2.ToShortDateString());
                DropDownList3.Items.Add(Q3.ToShortDateString());
                DropDownList3.Items.Add(Q4.ToShortDateString());

            }
0
 
devnewbeeAuthor Commented:
IJZ, I got your solution to work with:

     public List<DateTime> GetQuarter(DateTime startDate, int years)
        {
            List<DateTime> lst = new List<DateTime>();
            DateTime endDate = startDate.AddYears(3);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }
0
 
Imran Javed ZiaCommented:
That's nice
0
 
devnewbeeAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now