如何使列表框自动更新/刷新 [英] How Do I Make A Listbox Autoupdate/Refresh

查看:74
本文介绍了如何使列表框自动更新/刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框连接到一个sql表,该表预览表中的什么,所以当我添加到表中它显示在列表框中,公平地说它有效但只有当我重新加载它更新的表单时,继承人我的代码

i have a listbox conected to a sql table, the table previews whats in the table,so when i add to the table it shows in the listbox, fair enough it works but only if i reload the form it updates , heres my code

DataSet st = new DataSet();
            string strConnectionString = "Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True";

            SqlConnection objconnection = new SqlConnection(strConnectionString);
            using (SqlCommand cmd = new SqlCommand("SELECT  [name] FROM [staff1]",
            objconnection))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(st);
                }
            }

            var empList = st.Tables[0].AsEnumerable().Select(dataRow =>

            dataRow.Field<string>("name")).ToList();
            listBox1.DataSource = empList;

            listBox1.SelectedIndex = 0;
            listBox1.Refresh();

tried listBox1.Refresh(); almost everywhere , didnt help, 
also tried  public void RefreshItem2(int index)
            {
                RefreshItem(index);
            }



任何帮助都是apreciated:)


any help would be apreciated :)

推荐答案

您需要订阅更新通知来自数据库。搜索SqlDependency以及如何使用它来跟踪数据库更改。

另一种选择是使用计时器并定期检查新记录是否可用。在计时器已用事件上,您可以将数据绑定到您的控件。
you need to subscribe update notifications from database. search for SqlDependency and how you can use it to track database changes.
Another option is using timer and periodically check whether new records available or not. On timer elapsed event you can bind data to your control.


这篇关于如何使列表框自动更新/刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆