1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public static void fillComboBox(ComboBox comboName, string query, string dsTable, string display, string value)
{
comboName.Items.Add(string.Empty);
MySqlConnection SqlCnx = new MySqlConnection();
MySqlDataAdapter myAdapter = new MySqlDataAdapter();
DataSet ds = new DataSet();
SqlCnx.ConnectionString = "DataBase=prestige_app;DataSource=host;User Id=blah;Password=blah";
try
{
SqlCnx.Open();
myAdapter.SelectCommand = new MySqlCommand(query, SqlCnx);
myAdapter.Fill(ds, dsTable);
DataRow daRow = ds.Tables[dsTable].NewRow();
daRow["Valeur"] = "-";
daRow["ID"] = 0;
ds.Tables[dsTable].Rows.InsertAt(daRow, 0);
comboName.DataSource = ds.Tables[dsTable];
comboName.DisplayMember = display;
comboName.ValueMember = value;
}
catch (MySqlException Ex)
{
MessageBox.Show("DataBase Error (fillComboBox): " + Ex.Message, "DataBase Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
finally
{
myAdapter.Dispose();
ds.Dispose();
SqlCnx.Close();
SqlCnx.Dispose();
}
} |
Partager