如何优化这个任务?
我有四个组合框,我想用从同一个表中获取的相同数据填充它们,但此任务在袖珍电脑设备上需要花费大量时间。 所以我想知道是否有比这更快的方法:
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";
}
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";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您不应该向组合框添加大量项目;它很慢并且用户很难选择正确的项目。
我更喜欢使用用户控件以友好的方式显示我的数据。
如果这不是一个令人满意的更改,您可以在将项目加载到组合框中之前暂停表单布局:
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:
不,这是可以接受的。但是,我会将查询数据库的逻辑移至单独的类中。
No that's acceptable. However, I would move your logic of querying the DB into a separate class.