简体   繁体   中英

C# populate a combo box from a method

What I would like to do is to populate a drop down menu from a database.

First of all I plan to use a combo box.

I have created an object that contains the data that I need to take from the database. The object is as follows

namespace RLMD
{
public class FlashCardLevel
    {
        private int intFCLId;
        private String strFCLName;

        public FlashCardLevel(int intFCLId, String strFCLName)
        {
            this.intFCLId = intFCLId;
            this.strFCLName = strFCLName;
        }

        public int IntFCLId
        {
            get { return intFCLId; }
            set { this.intFCLId = value; }
        }

        public String StrFCLName
        {
            get { return strFCLName; }
            set { this.strFCLName = value; }
        }
    }
}

What I need to do is to add a list of items from a database, but for ease of use I have simulated given some sample data.

public List<FlashCardLevel> Rifle(List<FlashCardLevel> fcLevel)
{
    fcLevel.Add(new FlashCardLevel(1, "Severe"));
    fcLevel.Add(new FlashCardLevel(2, "Moderate"));
    fcLevel.Add(new FlashCardLevel(3, "Mild"));
    fcLevel.Add(new FlashCardLevel(4, "Slight"));

    return fcLevel;
}

I'm calling the method here.

List<FlashCardLevel> fcLevel = new List<FlashCardLevel>();
talkToDatabase.Rifle(fcLevel);
this.comboCardLevel.DataSource = fcLevel;
this.comboCardLevel.DisplayMember = "Name";
this.comboCardLevel.ValueMember = "Value";

The combobox is displaying no information.

I would appreciate any help

Updated:

The DisplayMember Property should be referring to FlashCardLevel (class), Property StrFCLName and ValueMember should be pointing to IntFCLId .

List<FlashCardLevel> fcLevel = new List<FlashCardLevel>();
talkToDatabase.Rifle(fcLevel);
this.comboCardLevel.DataSource = listFromDatabase;
this.comboCardLevel.DisplayMember = "StrFCLName";
this.comboCardLevel.ValueMember = "IntFCLId";

The DisplayMember and ValueMember should point to your properties name.

this.comboCardLevel.DisplayMember = "IntFCLId";
this.comboCardLevel.ValueMember = "StrFCLName";

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