Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Populate ComboBox from List
Message
 
 
To
21/06/2021 14:44:41
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01681491
Message ID:
01681502
Views:
30
Hi Dorris,

Did you try tracing the code in the debugger to see if it's populated correctly?

Also, few things I noticed which I would change:

1. No need to declare a string variable for the year and convert to int while you can use int directly

2. In your second sample, make sure the datatable is created and probably add ToList() at the end (You would need to add LINQ reference to get ToList() method )

>First thing I should mention, this is a windows form in a console app
>
>Now, having said that - I've got the following combobox that I want to have display 'this' year and the previous 3 years.
>
>So....
>
>    class YearList
>    {
>      public string YearNum { get; set; }
>      public int YearVal { get; set; }
>      public YearList(int v, string y)
>      {
>        YearVal = v;
>        YearNum = y;
>      }
>    }
>        string ThisYear = DateTime.Today.Year.ToString();
>        try
>        {
>          YearList[] list = new YearList[]
>          {
>          new YearList(0, "Select Year"),
>          new YearList(1,ThisYear),
>          new YearList(2, (Convert.ToInt32(ThisYear)-1).ToString()),
>          new YearList(3, (Convert.ToInt32(ThisYear)-2).ToString()),
>          new YearList(4, (Convert.ToInt32(ThisYear)-3).ToString())
>          };
>          cbYear.DataSource = list;
>          cbYear.DisplayMember = "YearNum";
>        }
>        catch (Exception em)
>        {
>          MessageBox.Show(em.Message);
>        }
>        this.ShowDialog();
>
>
>Generates no errors....also generates no display on the form.
>
>And whilst I'm here, I've got another combobox on the form that I'm trying to fill from a table with
>
>        DataTable Cnty = new DataTable("CntyTbl");
>        SqlDataAdapter CA = new SqlDataAdapter(Cntycmd);
>        CA.Fill(Cnty);
>        cbCnty.DataSource = Cnty;
>        cbCnty.DisplayMember = "TypeDesc";
>        cbCnty.ValueMember = "TypeCode";
>        cbCnty.Items.Insert(0, "all");
>        cbCnty.Items.Insert(0, "Select CSCD");
>
>
>Generates no errors....also generates no display on the form.
>
>So, what am I missing?
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform