I have two Dictionaries. One with a list of columns from an Excel sheet and a list of defined columns. I wanna know if the defined columns exists in the sheet. If they exists then they will selected in a chosen dropdownlist.
In the row dropdownList.SelectedValue = selectedItem.First().Key; I get sometimes an errormessage Sequence contains no elements. I thought that I had coded safety. What do I forget?
... the command ...
SetDataSource(import.ColumnList, import.DefColumnList, ddlSomeColumn, SomeEnum.Zipcode);
... and then the calling method ...
private void SetDataSource(Dictionary<int, string> columnList, Dictionary<int, string> defColumnList, DropDownList dropdownList, SomeEnum item)
{
int index = (int)item;
dropdownList.BeginUpdate();
dropdownList.ValueMember = "Key";
dropdownList.DisplayMember = "Value";
dropdownList.DataSource = columnList;
if (defColumnList.ContainsKey(index) && defColumnList[index].Length > 0)
{
var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]);
if (selectedItem != null)
dropdownList.SelectedValue = selectedItem.First().Key;
}
dropdownList.EndUpdate();
}