简体   繁体   中英

Visual Studio combo box dropdown list is only displaying 1 item/

I want my list to have 2 genders- boy and girl. I want the binded value to display automatically in the textbox, but both genders to display in the dropdown list.However, now I am only getting 1 gender in the dropdown list (the one binded).

this.person.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.Bind, "name1", true));
this.person.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.Bind, "name1", true));
this.person.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.Bind, "name1", true));
this.person.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.Bind, "name1", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.person.DataSource = this.Bind;
this.person.DisplayMember = "name";
this.person.FormattingEnabled = true;
this.person.Location = new System.Drawing.Point(360, 888);
this.person.Name = "FlagRMEdit";
this.person.Size = new System.Drawing.Size(1112, 271);
this.person.TabIndex = 551;

Lot's of ways to accomplish this. The easiest way is probably to hard code your options in the dropdown control on the aspx page itself, rather than doing it from code-behind.

    <asp:DropDownList runat="server" DataTextField="Text" DataValueField="Value">
<asp:ListItem Value="Value 1" Text="girl">Girl</asp:ListItem>
<asp:ListItem Value="Value 2" Text="boy">Boy</asp:ListItem>
 </asp:DropDownList>

I think you might be mixing data binding with the drop down values. Mind you, you can bind your drop-down list to a collection within .NET, but with such a static list of choices (Male, Female), a data-bound list of options is probably overkill.

The binding you listed will allow the choice in the combo box to bind automatically to the object, which is good. However, to control what items can be selected, you want the Items property of the combo box.

If this is a windows control, then it's simply Items :

comboBox1.Items.AddRange(new object[] { "Boy", "Girl"});

If it's a Dev Express control, then the Items Property is accessed via Properties :

comboBoxEdit1.Properties.Items.AddRange(new object[] { "Boy", "Girl"});

These both can be done through the designer, as well:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM