4886

Binding a dropdown with year

I have to bind a drop down box with years starting from 2008 to current year in C#. How can I achieve it.

Answer1:

You can build a sequence of integers with System.Linq.Enumerable.Range:

var startYear = 2008; myDropDownList.DataSource = Enumerable.Range(startYear, DateTime.Now.Year - startYear + 1); myDropDownList.DataBind();

Enumerable.Range on MSDN

<strong>Update:</strong> In .NET 2.0 you can implement your own Range operator with an iterator:

public static IEnumerable<int> Range (int start, int count) { int end = start + count; for (int i = start; i < end; i++) yield return i; }

Recommend

  • Using Range(Cells() Cells()).Style results in application defined or user defined error
  • adding a legend to a scatterplot with fitted line and prediction interval
  • TinyXML - any way to skip problematic DOCTYPE tag?
  • Can someone explain how Postgresql roles, Postgresql users and Linux users relate to each other?
  • Problem with sockets and OutOfMemory error
  • Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'Stri
  • draw9patch just hangs on Mac 10.7.5
  • Android SAXParser, parse into array and get child nodes
  • Entity Framework - Include in sub query? - Part 2
  • Difference between File>Close and :close in Vim
  • How can i create a big file in google drive with google app script?
  • How to add header and Footer to each activity in android
  • jQuery & CSS - Cut text by height, no truncate
  • ruby - lazily iterate through an array
  • Entity Framework 4.1. Updating many-to-many relationships. Is this the right way?
  • Is storing an OAuth token in cookies bad practise?
  • How can I encode a filename according to RFC 2231?
  • Underlying type for Tuple in Swift
  • Sum values in array of hash if they have the same value
  • How to Divide an array on c#?
  • How to get month name with year and list of years between two Date
  • Many to Many in Linq using Dapper
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • Multiple Left Join LINQ-to-entities
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • Exception creating JSON with LINQ
  • Linq Merge lists
  • Excel's Macro-Recorder usage
  • ActiveRecord query for a count of new users by day
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • How to handle AllServersUnavailable Exception
  • VBA Convert delimiter text file to Excel
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Reading document lines to the user (python)