简体   繁体   中英

Adding database column to combo box in windows form

In my winodws application, i want to add one column of a table in combo box.

The code below gets the Product Name from the database as dataset.

public DataSet GetAllItems()
{
    DataSet dataSet = new DataSet();
    // Create connection object
    OleDbConnection oleConn = new OleDbConnection(connString);
    try
    {
        oleConn.Open();
        string sql = "SELECT [Product Name] FROM [Product]";
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
        dataAdapter.Fill(dataSet, "Product");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        oleConn.Close();
    }
    if (dataSet.Tables.Count <= 0)
        return null;
    else
        return dataSet;
}

Now how can i add those items in dataset to the combo box.

Try:

DataSet ds = GetAllItems();
YourcomboBox.DataSource = ds.Tables[0];
YourcomboBox.DisplayMember = "Product Name";

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