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 ?
Yes you can bind an array but you have to remove DataTextField
and DataValueField
attributes
<asp:DropDownList
ID="DropDownList_FontColor"
runat="server">
</asp:DropDownList>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With