Korzystanie kod Jurija Faktorowicz jest powyżej jako podstawa, tutaj jest to, jak uzyskać listę dat w formacie LongDateString dla danej liczby tygodni i przypisać je do pola kombi. Używa „Poniedziałek”, ale można po prostu zastąpić „Poniedziałek” z jakiejkolwiek innej DOW do własnych celów:
private void PopulateSchedulableWeeks()
{
int WEEKS_COUNT = 13;
List<String> schedulableWeeks = PlatypusUtils.GetWeekBeginnings(WEEKS_COUNT).ToList();
BindingSource bs = new BindingSource();
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
}
public static List<String> GetWeekBeginnings(int countOfWeeks)
{
// from http://stackoverflow.com/questions/6346119/datetime-get-next-tuesday
DateTime today = DateTime.Today;
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7;
DateTime nextMonday = today.AddDays(daysUntilMonday);
List<String> mondays = new List<string>();
mondays.Add(nextMonday.ToLongDateString());
for (int i = 0; i < countOfWeeks; i++)
{
nextMonday = nextMonday.AddDays(7);
mondays.Add(nextMonday.ToLongDateString());
}
return mondays;
}
... a jeśli chcesz dodać faktyczną datę do pola kombi, zbyt, ty może używać takiego słownika:
int WEEKS_TO_OFFER_COUNT = 13;
BindingSource bs = new BindingSource();
Dictionary<String, DateTime> schedulableWeeks = AYttFMConstsAndUtils.GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT); bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
public static Dictionary<String, DateTime> GetWeekBeginningsDict(int countOfWeeks)
{
DateTime today = DateTime.Today;
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7;
DateTime nextMonday = today.AddDays(daysUntilMonday);
Dictionary<String, DateTime> mondays = new Dictionary<String, DateTime>();
mondays.Add(nextMonday.ToLongDateString(), nextMonday);
for (int i = 0; i < countOfWeeks; i++)
{
nextMonday = nextMonday.AddDays(7);
mondays.Add(nextMonday.ToLongDateString(), nextMonday);
}
return mondays;
}
Co widzisz? Czy przypisałeś właściwość DisplayMember? –