Dear All,
Please Help Me i want to write a code in asp.net using c# to list previous, current and next 3 years in dropdownlist dynamically at page load without using table.
Thanks and regards,
Dipti
Please help me to write a code to list only years dynamically
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Pasan Ratnayake100 reputationPosted Sep 5, 2010, 9:33 PM EDT
This is actually of no problem to be helped. This is pretty easy to attain if you looked at the 3 basic things: form_load event, adding/removing items in a combobox, getting current year. Make sure you try something before you post for help =]
int previousYearsCount = 3;
int afterYearsCount = 3;
comboBox1.Items.Clear();
int currentYear = DateTime.Now.Year;
for (int addedYear = currentYear - previousYearsCount; addedYear <= currentYear + afterYearsCount; addedYear++)
{
comboBox1.Items.Add(addedYear);
}
This is simple logic where you can use in your form load event.
Hope this helps.
Suthish NairRank #92 · 22.8K reputationPosted Sep 6, 2010, 12:56 PM EDT
int i = -1;
do
{
comboBox1.Items.Add(DateTime.Now.AddYears(i).Year.ToString());
i = i + 1;
} while (i<4);