how to optimize this task?
-
08-01-2021 - |
Question
i have four comboboxs, i want to fill them with the same data brought from the same table, but this task takes a lot of time on a pocket pc device. So i wonder if there is a way faster than this :
private void autreform_Load(object sender, EventArgs e)
{
DataTable dtable = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("select designation, num_produit from STK_PRODUITS_GENERIQUE where num_famille in (select num_famille from parametrage_vidange where produit='autres') ", mySqlConnection1);
adapter.Fill(dtable);
try
{
remplircombo(comboBox1, dtable);
remplircombo(comboBox2, dtable);
remplircombo(comboBox3, dtable);
remplircombo(comboBox4, dtable);
}
catch (Exception excr) { MessageBox.Show(excr.Message); }
}
private void remplircombo(ComboBox combo, DataTable dtable )
{
combo.DataSource = new BindingSource(dtable, null);
combo.DisplayMember = "designation";
combo.ValueMember = "num_produit";
}
La solution
Usually you should not add a lot of items to a combobox; It's slow and it gets hard for users to pick the correct item.
I prefer using an user control to show my data in a friendly way.
If it's not a satisfiable change you could suspend form layout before loading the items into the combobox:
try
{
this.SuspendLayout();
}
finally
{
this.ResumeLayout();
}
Autres conseils
No that's acceptable. However, I would move your logic of querying the DB into a separate class.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow