6

A question that I have never solved. I will illustrate with two code samples in which one works and the other doesn't:

Page_Load()
{
        FontFamily[] oFamilyFontList = FontFamily.Families;
        DropDownList_Fonts.DataSource = oFamilyFontList;
        DropDownList_Fonts.DataBind();

        string[] colorName = System.Enum.GetNames(typeof(KnownColor));
        DropDownList_FontColor.DataSource = colorName;
        DropDownList_FontColor.DataBind();
}
    <asp:DropDownList 
        ID="DropDownList_Fonts" DataTextField="Name" 
        DataValueField="Name" runat="server" >
    </asp:DropDownList>

    <asp:DropDownList 
        ID="DropDownList_FontColor"  DataTextField="colorName" 
        DataValueField="colorName" runat="server" >
    </asp:DropDownList>

The first DropDownList populates fine without any errors because each object oFamilyFontList has a property 'Name' which binds with the DataText and DataValue fields.

The second one has no properties at all and it's just an array of strings. What possibly can I put inside both fields to make it work ?

2
  • Maybe a stupid question - but - are there any values in colorName array? Commented Dec 7, 2011 at 9:19
  • Ofcourse there is, AVD have answered my question. Commented Dec 7, 2011 at 9:21

1 Answer 1

6

Yes you can bind an array but you have to remove DataTextField and DataValueField attributes

<asp:DropDownList 
        ID="DropDownList_FontColor"
        runat="server">
</asp:DropDownList>
Sign up to request clarification or add additional context in comments.

2 Comments

i really feel stupid. Thank you very much for the answer, it worked.
@yahyakh - There is nothing stupid in it. You should never hesitate to ask for help when it's clearly needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.