/// <summary> /// Selects an item in the ListItemCollection where the date specified is in the date format. /// </summary> /// <param name="listItems">the ListItemCollection in which to perform selection</param> /// <param name="dateString">The date string</param> /// <param name="displayDateFormat">The display format of the list item dates.</param> /// <returns> /// The number of items selected. This is either 0 or 1. /// </returns> private int PerformInitialSelectionForDate(ListItemCollection listItems, string dateString, string displayDateFormat) { //Throw error if InitialSelection is empty string and list contains some items. if (InitialSelection.Trim().Equals(String.Empty) && listItems.Count > 0) { throw new InitialSelectionInvalidDataException("InitialSelection can not be an empty string."); } //Try each list item for each date foreach (ListItem item in listItems) { DateTime itemDate, selectDate; try { itemDate = DateTime.ParseExact(item.Value, displayDateFormat, CultureInfo.InvariantCulture); selectDate = DateTime.ParseExact(dateString.Trim(), DateFormat, CultureInfo.InvariantCulture); } catch (Exception e) { throw new InitialSelectionInvalidDataException("Could not convert date string to date.", e); } //Item must not be selected from before if (!item.Selected && itemDate == selectDate) { item.Selected = true; return(1); } } return(0); }