How to display columns from a database table in a combobox?

my table name is Students with columns ID, Name, ...


Answer this question

How to display columns from a database table in a combobox?

  • Kdbryan

    Ice,

    If you are using the designer...

    Right-Click on the datagrid, select Edit Columns, click on the "Name" column in the field list. Click on "ColumnType" under the "Design" heading. Select DataGridViewComboBoxColumn for the type.

    Then click on DataSource, and select the Student table. Click on the DisplayMember and select whatever field you wish to be in the combo list. Be aware that if several of the same values are in the field, you will get duplicates in your combo list.

    Good luck

    G.


  • jbujold

    What about if I'm using SQL Server 2005



  • Harrives

    What I want to display are the column names not their values.

  • Mark Kucera

    The function getDBInfo return the table colume name  by string [] name and the table colume count by count;:

    To run this function, you should connect the database with the commStr first.

    Then call it like getDBInfo(tableName, out ModelNum,"SHOW TABLES");

    The database is mysql.

    public void getDBInfo(string[] name, out int count,string commStr)
            {

                count=0;
                myComm.CommandText = commStr;
                MySqlDataReader reader = null;
                try
                {
                    reader = myComm.ExecuteReader();
               
                    while (reader.Read())
                    {
                        name[count] = reader.GetString(0);
                        count++;
                    }
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show("Failed to populate table list: " + ex.Message);
                }
                finally
                {
                    if (reader != null) reader.Close();
                }
            }



  • Endmaker

    Hello

    Create a DataSource, Set the combo box datasource property to the created datasource, then set the combo box displaymember property to "Name" and valuemember property to "ID", so in this way all the names will be populated in the Combo Box.



  • How to display columns from a database table in a combobox?