Jayseer
Jayseer

Reputation: 199

How do you select data from MySQL database for it to be used in Combo Box Selection?

So usually when you create a combo box you'll be the one to put the value of the selection but I want the data in my combo box will be selected from my database mysql. How am I going to do that?

I am stuck at selecting data from my sql to combo box!

Here's my code so far!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MySqlConnection connection = null;            
            string hostname = "localhost";
            string database = "aparece_hoteldb";
            string username = "root";
            string password = "";
            connection = new MySqlConnection("host=" + hostname + 
                                            ";database=" + database + 
                                            ";username=" + username + 
                                            ";password=" + password + ";");


            string table = "reservations";
            string query = "SELECT * FROM " + table;
            connection.Open();
            MySqlDataAdapter da_res = null;
            DataSet ds_res = null;
            ds_res = new DataSet();
            da_res = new MySqlDataAdapter(query, connection);
            da_res.Fill(ds_res, table);

            dataGridView2.DataSource = ds_res.Tables[table];

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Upvotes: 1

Views: 4257

Answers (3)

Roden Pajo
Roden Pajo

Reputation: 3

//inside the FORM_LOAD,

private void Form7_Load(object sender, EventArgs e){
        cboFloor.SelectedIndex = -1;
        cboFloor.DropDownStyle = ComboBoxStyle.DropDownList;
        GetFloor();
        }

//Create a Function and insert this code.

private voide GetFloor{
        string query = @"SELECT DISTINCT floor FROM tbroom ORDER BY floor ASC";



        try
        {
            con.Open();
            MySqlCommand cmd = new MySqlCommand(query, con);
            MySqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                cboFloor.Items.Add(rdr["floor"]);

            }
        }
        catch (MySqlException mysqlex)
        {
            MessageBox.Show(mysqlex.Message.ToString());
        }
        finally
        {
            con.Close();
        }}

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

OK, so binding a list of data to a combo box, especially when it's one you've already got, is going to be pretty straight forward. So, after this line:

dataGridView2.DataSource = ds_res.Tables[table];

let's add a few more:

comboBox1.DisplayMember = "YourDisplayField";
comboBox1.ValueMember = "YourValueField";
comboBox1.DataSource = ds_res.Tables[table];

and that will bind the data to the combo box. But let's break this down. The DisplayMember is the field value that you want the user to see. Often times this is a name or a brief description of the row. The ValueMember is the field you want bound to the SelectedValue property. When the user selects an item in the combo box the SelectedValue will be set to the value of that field.

Now you can consume a better event that SelectedIndexChanged, now you can consume SelectedValueChanged. Every time the user selects a new value that event will fire and you can do with it what you need.

You could get the actual DataRow if you wanted by casting the SelectedItem property of the combo box like this:

var row = comboBox1.SelectedItem as DataRow;

or you could just grab that value and do something with it:

var val = comboBox1.SelectedValue;

and you could cast that to whatever type the ValueMember field is. If you set it to an int field then you might do something like this:

var val = (int)comboBox1.SelectedValue;

if it's a string field then maybe something like this:

var val = comboBox1.SelectedValue as string;

Upvotes: 3

CathalMF
CathalMF

Reputation: 10055

Assuming you get a single column returned by your sql you could set the datasource of the combobox to populate it from the datatable or dataset.

comboBox1.DataSource = myDataTable;

Upvotes: 1

Related Questions