2010-08-18 2 views
1

지난 달의 몇 일전에서 현재 달까지의 달과 연도가있는 드롭 다운 목록이 필요합니다. 지난 달은 현재 달이 될 것이다, 그래서지난 달에서 현재 월까지의 월 및 일수 목록 작성 방법

for (int year = 2009; year <= DateTime.Now.Year; year++) 
{ 
    for (int month = 1; month <= 12; month++) 
    { 
     DateTime date = new DateTime(year, month, 1); 
     this.MonthsCombo.Items.Add(
      new RadComboBoxItem(date.ToString("Y"), 
           String.Format("{0};{1}", year, month))); // this is for reading selected value 
    } 
} 

어떻게 그 코드를 변경하려면 : 이 내 코드?

답변

2

ity가 오늘보다 작 으면 값을 추가하십시오.

if (date <= DateTime.Today) 
{ 
    this.MonthsCombo.Items.Add( 
      new RadComboBoxItem(month.ToString("Y"), 
           String.Format("{0};{1}", year, m))); // this is for reading selected value 
} 

또한, 나는, while 루프의

DateTime startDate = new DateTime(2009, 01, 01); 
while (startDate <= DateTime.Today) 
{ 

    startDate = startDate.AddMonths(1); 
} 
+0

'다른 휴식 같은 것을 만들 것이다'너무 도움이 될 수 있습니다. – shahkalpesh

+0

사실입니다. 편집 된 버전을 참조하십시오. –

+0

루프는 괜찮지 만 startDate = startDate.AddMonths (1); 그렇지 않으면 무한 루프입니다 – jlp