Binding a string array to a DropDownList?

A question that I never solved. I will illustrate with two code examples in which one works and the other does not:

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 fills everything without errors, because each oFamilyFontList object has a "Name" property that is associated with the DataText and DataValue fields.

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

+6
source share
1 answer

Yes you can bind an array, but you need to remove the DataTextField and DataValueField

 <asp:DropDownList ID="DropDownList_FontColor" runat="server"> </asp:DropDownList> 
+6
source

Source: https://habr.com/ru/post/903272/


All Articles