views:

24

answers:

2

Hello. I have an int variable, e.g. int i = 100;

What I want to do is binding a ddl with 100 listitems, from 1 to 100. I could cycle the variable and for each number adding a ListItem to the ddl, but I'd like to know if there's an alternative, something like value the DataSource with the variable.

Thanks

+3  A: 
int startingItem = 1;
int numberOfItems = 100;
IEnumerable<int> bindingSource = Enumerable.Range(startingItem, numberOfItems);
Anthony Pegram
Very nice solution, thanks!
opaera
Cool, didn't know about this method +1
Dave
A: 

If the text and value of each ListItem should be the same just use:

myDropDownList.DataSource = myListOfInts;
myDropDownList.DataBind();

Alternatively, you can use Linq for a more complex setup

myDropDownList.DataSource =
    from i in myListOfInts
    select new ListItem("My Num: " + i, i.ToString());
myDropDownList.DataBind();
Michael La Voie