opaera
opaera

Reputation: 21

Bind dropdownlist with integers

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

Upvotes: 2

Views: 2425

Answers (2)

Michael La Voie
Michael La Voie

Reputation: 27926

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();

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126932

int startingItem = 1;
int numberOfItems = 100;
IEnumerable<int> bindingSource = Enumerable.Range(startingItem, numberOfItems);

Upvotes: 7

Related Questions