Populating DropDownList in GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    #1

    Populating DropDownList in GridView

    Hi again,

    Okay, I decided the GridView is the way to go to solve my problem. But now I've got another problem.

    I've found plenty of examples about how to add a DropDownList item to the grid view, how to bind it and how to add the selection items to the list. My problem is that in all the examples I can find all show how to add the selection items from a database only. I don't always want to do that. Sometimes I want to add just from a predefined list of values.

    For example:
    Code:
                    <asp:TemplateField HeaderText="Color">
                        <ItemTemplate>
                            <asp:DropDownList DataSource="<%# GetColors() %> 
                            DataTextField="Color" DataValueField="Color" 
                            ID="ddlColor" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
    (I haven't done the binding step yet). In the examples I've found, the GetColors() method selects from a database. Instead, I want to simply have the selections 'RED', 'BLUE', 'GREEN' only. Whatever the user enters will eventually get stored to the database via the binding.

    Help! Again, I ask your patience as I'm rather new to this.

    Robert
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Robert,

    You can create a ListItem between the DropDownList tags for each option you want. Take a look at the following example:
    Code:
    <asp:DropDownList ID="ddColour" runat="server">
    	 <asp:ListItem Text="RED" Value="0" />
    	 <asp:ListItem Text="GREEN" Value="1" />
    </asp:DropDownList>
    I'm sure you can see how this works but let me know if you need any more assistance.

    Hope this helps,

    Dr B

    Comment

    • RobertTheProgrammer
      New Member
      • Aug 2007
      • 58

      #3
      Thanks for the response. Works perfectly. I'm still learning the nuances on this GridView stuff, I really appreciate you taking the time.

      Robert

      Comment

      Working...