none
Binding an array to a dropdownlist in c# RRS feed

  • General discussion

  • Hello,

    I have an array in my code behind page. say:

    int[] Hour = new int[24];

    I also have a dropdownlist on my aspx page. say:

    <asp:dropdownlist id="ddlHour" runat="server">

    I want to bind the value of array to the dropdownlist such that when I dropdown the list, I get to see the values from 0 to 24. Any idea as to how should I move ahead?

    Thanks in advance,

    Vernon

    Friday, December 3, 2010 7:49 AM

All replies

  • Hi Vernon,

     

    I was wondering if you have initialized the array with the values from 0 to 24? If you take a look at http://msdn.microsoft.com/en-us/library/0a7fscd0.aspx you can see there that you create the new array but that it will contain the default values after that.

     

    You can also take a look at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.datasource.aspx to see how to databind your array to the dropdownlist.

     

    Wouter :)

    Friday, December 3, 2010 7:59 AM
  • Wouter, do I need to explicitly initialize the array from 0 to 24? or can I use a for loop which will count from 0 to 24. say:

    for (int i=0; i <=24, i++)

    {

    Hour[i];

    }

    and finally binding Hours[i] to the dropdownlist? Is this possible at all? Also, the link you provided expains binding a gridview control to a dataset. Is this the same way we bind a dropdownlist to an array? Please let me know if the question is clear. and also if you can help :)

    Vernon

    Friday, December 3, 2010 8:13 AM
  • Hi Vernon,

     

    using the loop is perfect :) Make sure you don't make any mistakes with the zero based index ofter the loop! 

    int[] array = new int[5];
    
    

    This array contains the elements from array[0] to array[4]

     

    DataSource can databind to an enumerable. In the example a dataset is used but you can also do it with a list, or an array.

     

    Wouter :)

     

    Friday, December 3, 2010 8:16 AM
  • Perfect! Thanks :)

    Vernon

    Friday, December 3, 2010 8:25 AM
  • Because this is a discussion and not a question. 

    In my idea you are using the meaning of binding wrong. 

    Binding is to bind a type to a property. That can be early binding or late binding.

    Maybe you mean DataBinding, but that is senseless in a drowdownlist because the values in a dropdownlist cannot be changed.

    So what you need to do is to set the items of the dropdownlist to the array.

     


    Success
    Cor
    Friday, December 3, 2010 8:35 AM
  • Thanks..

    I understand that the best way is to perform either of the two mentioned below:

    int[] Hour = new int[24];

    for (int i=0; i <=24, i++)

    {

    Hour[i];

    }

    ddlHour.DataSource = Hour[i];
    ddlHour.DataBind();

    (or)

    int[] Hour = new int[24];

    for (int i=0; i <=24, i++)

    {

    ddlHour.Items.add(Hour[i]);

    }

    Vernon

    Friday, December 3, 2010 9:49 AM
  • Hi Vernon,

     

    you need to change your array init to:

    int[] Hour = new int[24];
    
    for (int i = 0; i < 24; i++)
    {
    	Hour[i] = i;
    }

    When doing the databind, don't bind to a specific item but to the complete list.

    But as Cor pointed out, using the Items property is better.

    Hope this helps,

    Wouter

    Friday, December 3, 2010 9:56 AM
  • Hi Vernon,

                 Below is the code snippet to bind an array with dropdownlist

                   object[] obj = { 1,2,3,4};

                this.DropDownList1.DataSource = obj;

                this.DropDownList1.DataBind();

                All the best :)

                Regards,

                 Sabeek Vasan

    Friday, December 3, 2010 11:40 AM
  • You can use the

    ddlHour.Items.AddRange( Hour )

    to avoid the loop

    Thursday, October 6, 2016 1:54 PM