I have a C#.net winforms application which uses Entity Framework code first to connect to SQL DB.
One of my combo boxes loses the list of values that its supposed to pull from an enumeration (list of the 12 months) and it just shows the name of the month list object.
It happened since I changed code which is completely unrelated to the combo and should not affect it (moved some unrelated code to another class) and it seems to have caused the issue above. Something similar happened previously when I added some new code it caused the same thing to happen. Eventually it fixed itself after i changed a linq query to return a "TBC" string when a certain value was null. Bearing in mind the code changed has nothing to do with the combo box in question. The combo is also populated before any of the unrelated code ever runs.
This is the code that populates the combo:
private void UpdateMonthCombo()
{
ArrayList AL = new ArrayList();
foreach (string cs in Enum.GetNames(typeof(MonthsEnum)))
{
MonthList aEnum = new MonthList((int) Enum.Parse(typeof(MonthsEnum), cs), cs);
AL.Add(aEnum);
}
BindingSource source = new BindingSource();
source.DataSource = AL;
comboMonth.ValueMember = "_MonthNumber";
comboMonth.DisplayMember = "_Name";
comboMonth.DataSource = source;
comboMonth.SelectedItem = null;
}
I'd love to know how this can happen and how to fix and prevent it. I'm sure you can tell that i'm relatively new to C#!
Any help is much appreciated.
This is MonthList class:
public class MonthList
{
public string _Name { get; set; }
public int _MonthNumber { get; set; }
public MonthList(int MonthNumber, string Name)
{
_Name = Name;
_MonthNumber = MonthNumber;
}
public enum MonthsEnum : int
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
}
Tried using this instead of the enumeration. Tested on separate form works as expected. But on my form it only displays the month numbers:
private void UpdateMonthCombo()
{
var monthList = CultureInfo.InvariantCulture.DateTimeFormat.MonthNames.Select((item, index) => new
{
Month = item,
Value = index + 1
});
var result = monthList.Where(m => m.Month != "");
comboMonth.DataSource = result.ToList();
comboMonth.ValueMember = "Value";
comboMonth.DisplayMember = "Month";
}
I refactored my selectedindexchanged event on the combo box. It fixed the combobox. But now the main gridview is broken. It seems related to a method which adds a combobox column. When i comment out this method the gridview shows all columns (no combo column) but the original comboMonth becomes broken again showing just the month numbers:
private void AddProjectCombo()
{
gridBusinessKMs.Columns["Project"].Visible = false;
DataGridViewComboBoxColumn cmbCol = new DataGridViewComboBoxColumn();
cmbCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cmbCol.HeaderText = "Project";
cmbCol.Name = "Project ";
cmbCol.Items.Add("True");
using (var context = new MileageLogDBContext())
{
cmbCol.DataSource = (from p in context.Projects
orderby p.ProjectDescription
select p.ProjectDescription).ToList();
}
gridBusinessKMs.Columns.Add(cmbCol);
gridBusinessKMs.Columns["Project "].DisplayIndex = 1;
}