Filling in the drop-down list with months and numbers for several months

I am working on a WPF project and I have a monthly summary that should contain the months from January to December. I managed to do this by running the following snippet:

var americanCulture = new CultureInfo("en-US"); ddlMonth.Items.AddRange(americanCulture.DateTimeFormat.MonthNames); 

And got a list like:

 January February March .... 

But I want them to be populated as:

 01 - January 02 - February 03 - March ..... 

Any ideas how to do this for the loop? I am new to C #. Thanks

+4
source share
4 answers

Oi. Why are you doing this in the backend. Use wpf and use xaml.

Let me explain how to do this.

  • First create a dependency property for months. If you have not used dependency properties. You should research them now http://wpftutorial.net/DependencyProperties.html . In the code below, I set the default value of the property to the list of months through the new PropertyMetadata (new CultureInfo ("en-US"). DateTimeFormat.MonthNames.Take (12) .ToList ())). So now we can get this property from xaml.

     public partial class MainWindow : Window { public static readonly DependencyProperty MonthsProperty = DependencyProperty.Register( "Months", typeof(List<string>), typeof(MainWindow), new PropertyMetadata(new CultureInfo("en-US").DateTimeFormat.MonthNames.Take(12).ToList())); public List<string> Months { get { return (List<string>)this.GetValue(MonthsProperty); } set { this.SetValue(MonthsProperty, value); } } public MainWindow() { InitializeComponent(); } 

    }

  • You need a converter. http://wpftutorial.net/ValueConverters.html if they are not familiar with them. What the converter will do is for each value in the list that we are going to change to get the desired result. So, create a new class for the value converter.

     [ValueConversion(typeof(List<string>), typeof(List<string>))] public class MonthConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { //get the list of months from the object sent in List<string> months = (List<string>)value; //manipulate the data index starts at 0 so add 1 and set to 2 decimal places if (months != null && months.Count > 0) { for (int x = 0; x < months.Count; x++) { months[x] = (x + 1).ToString("D2") + " - " + months[x]; } } return months; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion 

    }

  • Install it in xaml. Create a name for your window, i.e. X: Name = "testWindow", which means that it can better access it when binding. Customize the namespace to get the converter. XMLNS: Main = "CLR Names: WpfApplication4". Add the converter to resources. In combobox, bind the itemssource to the Months dependency property and send it through the converter.

     <Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="testwindow" xmlns:Main="clr-namespace:WpfApplication4" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Main:MonthConverter x:Key="MonthConverter"/> </Window.Resources> <Grid> <ComboBox ItemsSource="{Binding ElementName=testwindow,Path=Months, Converter={StaticResource MonthConverter}}" HorizontalAlignment="Left" Margin="197,107,0,0" VerticalAlignment="Top" Width="120"/> </Grid> </Window> 
+6
source
  var americanCulture = new CultureInfo("en-US"); var count = 1; //.Take(12) in the foreach below because apparantly MonthNames returns 13 //elements of which the last one is empty foreach (var month in americanCulture.DateTimeFormat.MonthNames.Take(12)) { DropDownList1.Items.Add(new ListItem{Text = count.ToString("00") + "-" + month}); count++; } 
+4
source

Try the following:

  List<string> names = new List<string>(); int num = 1; foreach (var item in americanCulture.DateTimeFormat.MonthNames) { names.Add(string.Format("{0} - {1}", num++.ToString("D2"), item)); } ddlMonth.Items.AddRange(names); 
+2
source

The best way to do this is to create a combo box per day and a combo box for a month.

Filling the month and day

uses

 for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++) { cmbDay.Items.Add(i.ToString()); } 
+2
source

All Articles