Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to Fill Combobox with All Month Names With values can u guide or send snippets
Posted
Updated 31-May-12 0:24am
v2
Comments
Rajesh Kariyavula 31-May-12 5:07am    
What do you mean by "dynamically". Due you want to load month names based on a criteria or load all months.

As VJ reddy wrote, you can use the DateTimeFormatInfo to get that information, but if you want to get value of selected month use simple trick:
C#
int iVal = ComboBox.SelectedIndex + 1; //if you select February, returns 2


[Comment]Thank you, VJ for your comment[/Comment]
 
Share this answer
 
v2
Comments
VJ Reddy 31-May-12 10:38am    
Good point. 5!
I think SelectedIndex+1 will do.
Maciej Los 31-May-12 11:15am    
Thank you, VJ ;)

I think SelectedIndex+1 will do. - Yes, you're right. Sorry, my mistake.
Member 12595442 22-Jun-16 8:37am    
hi I want code for email sending for password reset with the proper explanation
The MonthNames property of DateTimeFormatInfo can be used to get a list of Month names as shown below:
C#
protected void Page_Load(object sender, EventArgs e)
{
    var months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames;
    DropDownList1.DataSource = months;
    DropDownList1.DataBind();
}
 
Share this answer
 
Comments
MAKReddy 31-May-12 6:34am    
hi its good here we can get month names but if i select i want to take value how we will get value in windows combobox.
Maciej Los 31-May-12 9:41am    
See my answer.
Maciej Los 31-May-12 9:35am    
Good answer, my 5!
VJ Reddy 31-May-12 10:37am    
Thank you, losmac :)
ujju.1 1-Jun-12 2:31am    
good one...5
Hello,
here is the code-
VB
Dim i As Integer = 1
            Do While i <= 12
                myvar = MonthName(i, True)
                cboToMonth.Items.Add(New ListItem(myvar, i))
                i = i + 1
            Loop
 
Share this answer
 
Supposed you have a function to bind your data here;

C#
protected void BindMonthList(DropDownList ddl)
{
    var monthList = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames.Select((item, index) => new
    {
        Month = item,
        Value = index+1
    });
    ddl.DataSource = monthList;
    ddl.DataTextField = "Month";
    ddl.DataValueField = "Value";
    ddl.DataBind();
}
 
Share this answer
 
this is my solution:
C#
if (this.ddlMesi.Items.Count <= 0)
    for (int i = 1; i <= 12; i++)
    {
        ddlMesi.Items.Add(new ListItem(
            System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i),
            i.ToString()
        ));
    }
 
Share this answer
 
This tip might help you - Dynamic DropDownList with Month Names[^].
 
Share this answer
 
Comments
Member 10366594 5-Nov-13 0:59am    
how to bind the combobox selected month all dates into datagrid in wpf
C#
private void Form1_Load(object sender, EventArgs e)
       {
           var months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames;
           comboBox1.DataSource = months;

       }
 
Share this answer
 
Comments
Member 10282837 22-Sep-13 4:17am    
how to get default month in combo box
Dim month
Dim i = 1
Do While i <= 12
month = MonthName(i, True)
CmbMonth.Items.Add(New ComboBoxPairs(month, i))
i = i + 1
Loop
 
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