0

I've been searching for an answer, but haven't found one (proberly because I'm not familar enough with the right terminology and what to search for). Maybe I've already stumpled over the answer in my search, but just hasn't been able to understand it. I'm trying to populate a dropdownlist in one WinForm, using a Function from another form (edit: class). I've created a class with different functions, connecting, retrieving and manipulating a MySQL database. For this specific question, I need a DropDownList to populate with data retrieved from specific column in a specific database table, using the function, but I can't really wrap my head around what the data should stored in, and how to pass it to the DropDownList. Tried different things, like IList, and making the Funtion an Array. My logic tells my that the data should be stored like an Array, and the DropDownList should be populated with a ForEach Loop. The SQL part I'm okay with. It's just the logic with the storing I'm uncertain of. My Function looks like this at this time:

public IList<string> Populate_DropDowns(string Table, string Column_Name)
    {
        string query = "SELECT * FROM " + Table;
        IList<string> Populate_DropDowns = new List<string>();

        if (this.Open())
        {
            MySqlCommand cmd = new MySqlCommand(query, conn);
            MySqlDataReader dataReader = cmd.ExecuteReader();

            try
            {
                while (dataReader.Read())
                {                        
                        Populate_DropDowns.Add(dataReader[Column_Name].ToString());                            dataReader.Close();
                }

            }
            catch { }
            this.Close();

            return Populate_DropDowns;
        }
        else
        {
            return Populate_DropDowns;
        }
    }

In the Form with the DropDownList I have following code:

protected virtual void OnLoad(EventArgs e)
    {

            DropDownList_Select_Template.Items.Add(sqlClient.Populate_DropDowns(Table, Column_Name));

    }

I'm using C# WinForms.

Please advise. Any help is appreciated.

Best regards

Frank

Frank
  • 11
  • 1
  • 3
  • Possible duplicate: http://stackoverflow.com/questions/7423911/how-to-populate-c-sharp-windows-forms-combobox – ryrich Feb 27 '15 at 21:39
  • Also refer to http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms – ryrich Feb 27 '15 at 21:40
  • Thank you very much. I'll dig into those. Sorry for the duplicate. Not sure whether this question should/will be deleted? Br Frank. – Frank Feb 28 '15 at 14:33
  • I managed to solve my problem with arrays. Not sure if it's the best way, but it worked and I understood it :) – Frank Mar 23 '15 at 20:39

0 Answers0