Click here to Skip to main content
15,921,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to bind the 18 months to the Dropdownlist (with out manually) as follows:

From :January 2014(Name) -2014/1/1(Value) To June 2015(2015/06/1)

Please send the sample C# codes.

Thanks!
Posted
Comments
BillWoodruff 22-Sep-14 13:04pm    
You get started, and post some code, and ask some specific questions, and we'll help you learn.
priya9826 22-Sep-14 13:09pm    
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
DateTime startDte = Convert.ToDateTime(ConfigurationManager.AppSettings["DayPilotHousingStartDate"].ToString());
for (int i = 1; i < 15; i++)
{
//ddlMonth.Items.Add(new System.Web.UI.WebControls.ListItem(info.GetMonthName(i) + info.YearMonthPattern, info.ShortDatePattern.ToString()));
ddlMonth.Items.Add(new System.Web.UI.WebControls.ListItem(info.GetMonthName(i) + " " + startDte.Year, startDte.Year+"/" + i.ToString()+"/1"));
}

I have written the above code to display the Month.But It will works for only 12 months.How to bind the dropdownlist for 18 months?

I am just providing you with the for loop changes you have to make for 18 months.
C#
int mnth = 0, year = 0;
           for(int i = 1; i <= 18; i++)
           {
               if (i > 12)
               {
                   mnth = i - 12;
                   year = startDte.Year + 1;
               }
               else
               {
                   mnth = i;
                   year = startDte.Year;
               }
               ddlMonth.Items.Add(new ListItem(info.GetMonthName(mnth)+" "+year.ToString(), year.ToString() +"/"+mnth.ToString()+"/1"));
           }


Hope the above code helps.
 
Share this answer
 
When you use the word "bind," that has a specific technical meaning in that many .NET Controls that hold collections of various types have a DataSource Property to which you can assign a DataSet, or other structure, and then the Control's Items are automatically added from that DataSource.

I can't tell if 'ddlMonth is a WebControl ListBox, or a ListView, in your code, but both those Controls expose the 'DataSource property, and support binding.

So, if you are interested in using binding in this way, I suggest you review: [^].

If the Items to be added are always the same, that suggests generating them by computation, and ChauhanAjay's answer above can get you started on that.

If the items are going to vary depending on the content of a DataBase (like the result of a Query), then consider really exploring "binding" as presented here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900