0

I have a class library where i get the data i need for the combobox which is

public List<Consoles> fillConsole()
        {

            var consoles = new List<Consoles>();

            if (!DatabaseConnection.IsOpen)
            {
                DatabaseConnection.Open();
            }
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = DatabaseConnection.Connection;

            string str = "SELECT consolename FROM Console";

            cmd.CommandText = str;

            OracleDataReader dr = cmd.ExecuteReader();
            List<String> list = new List<string>();

            while (dr.Read())
            {
                var console = new Consoles();
                console.ConsoleName = dr["Consolename"].ToString();
                consoles.Add(console);
            }

                DatabaseConnection.Close();

                return consoles;
            }

i would like to know how i could add this data to the combo box in my form when the page is loaded all i seem to get in the combobox is either collection or System.Collections.Generic.List`1[ClassLibraryGameSite.Consoles

If my fill code is wrong also could someone please tell me what thanks.

Console Class:

public class Consoles
    {
        private int consoleId;
        private String consoleName;

        public String ConsoleName
        {
            get { return consoleName; }
            set { consoleName = value; }
        }

        public int ConsoleId
        {
            get { return consoleId; }
            set { consoleId = value; }
        }
    }
  • See this: http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – Jauch Dec 06 '14 at 19:18

1 Answers1

1

First of all your method have a weird name, it does not fill anything it only gets the values from the database so instead of fillConsoles it should be GetConsoles or GetConsolesFromDb but it is not the answer you're looking for.


The thing with the comboBox is that its datasource can be any kind of object so how we set what will be displayed in the comboBox text when it can be any property of any class or struct? we simply set the DisplayMember property of the ComboBox to whareever property you want to display in which case I guess it is the ConsoleName So it should be something like this

comboBox.DataSource = GetConsoles(); //or fillConsoles whichever is the name of your method
comboBox.DisplayMember = "ConsoleName";
Patrick
  • 736
  • 9
  • 27