MySQL
libraries
using MySql.Data.MySqlClient;
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbsubjects;sslMode=none"); MySqlCommand cmd; MySqlDataAdapter da; DataTable dt; String sql;
private void fill_combo(string sql) { try { con.Open(); cmd.Connection = con; cmd.CommandText = sql; da.SelectCommand = cmd; da.Fill(dt); foreach (DataRow dr in dt.Rows) { cbo.Items.Add(dr.Field<string>("SUBJ_CODE")); cbo.Name = "Subject Code"; } //add the combobox in the column in the datagridview dataGridView1.Columns.Add(cbo); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } finally { con.Close(); da.Dispose(); } }
private void Form1_Load(object sender, EventArgs e) { sql = "SELECT * FROM subject"; fill_combo(sql); }