你如何在 C# 中每分钟更新一个 datagridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/603929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you update a datagridview in C# every minute
提问by Daniel Mitchell
I am working on a project in C# at the moment which is quite simple.
我目前正在使用 C# 开发一个非常简单的项目。
I have a status box, two buttons and a dataGridView.
我有一个状态框、两个按钮和一个 dataGridView。
When the Form loads the dataGridView is filled correctly.
当表单加载时 dataGridView 被正确填充。
What I would like to do is then update that table every 45 seconds to reflect any changes in the database.
我想做的是每 45 秒更新该表以反映数据库中的任何更改。
I am looking for suggestions on a technique to achieve this. I have been searching for clear information but it seems somewhat lacking.
我正在寻找有关实现此目标的技术的建议。我一直在寻找明确的信息,但似乎有些缺乏。
采纳答案by Daniel LeCheminant
- Add a
Timer
control to your form. (It's in the components category) - Set its
Interval
property to45000
(the value represents milliseconds) - Either set the
Enabled
property of the timer toTrue
in the form designer, or somewhere in your code. - Add a handler for the timer's
Tick
event (you can get this by double-clicking the timer) - Inside the
Tick
handler, update yourdataGridView
Timer
向表单添加控件。(它在组件类别中)- 将其
Interval
属性设置为45000
(该值代表毫秒) Enabled
将计时器的属性设置为True
在表单设计器中或代码中的某处。- 为定时器的
Tick
事件添加一个处理程序(你可以通过双击定时器来获得) - 在
Tick
处理程序内,更新您的dataGridView
Your handler will look like this:
您的处理程序将如下所示:
private void timer1_Tick(object sender, EventArgs e)
{
// Update DataGridView
}
If you need to suspend updates for some reason, you can call timer1.Stop()
to stop the timer from running, and use timer1.Start()
to start it up again.
如果您出于某种原因需要暂停更新,您可以调用timer1.Stop()
停止运行计时器,并使用timer1.Start()
它再次启动它。
回答by TheTXI
Use a timer control and then perform your updates at the specific increments you need.
使用计时器控件,然后以您需要的特定增量执行更新。
回答by Andy
回答by BFree
Like others have suggested, use a Timer to requery the Database. The only thing I'd like to add is that when you re-query the database, don't just set the DataGridView's datasource to the new table. Rather, Merge it with the existing table. The reason for this is because if the user is in middle of the grid for example looking at a particular row, if you reset the DataSource to a new table, the entire grid will refresh and they will lose their place. Annoying as hell! If you mergeit though, it will be seamless to the user.
像其他人建议的那样,使用计时器重新查询数据库。我唯一想补充的是,当您重新查询数据库时,不要只是将 DataGridView 的数据源设置为新表。相反,将其与现有表合并。这样做的原因是因为如果用户位于网格中间,例如查看特定行,如果您将 DataSource 重置为新表,则整个网格将刷新并且它们将丢失其位置。烦死了!如果您合并它,它将对用户来说是无缝的。
The one thing to be aware when using the Merge method is that the table needs to have a primary key. Double check to make sure that the DataTable itself has a primary key. Not always does it pull it back from the database. You may need to do something like:
使用 Merge 方法时要注意的一件事是表需要有一个主键。仔细检查以确保 DataTable 本身具有主键。它并不总是从数据库中取回它。您可能需要执行以下操作:
table.PrimaryKey = new DataColumn[] {table.Columns["ID"]};
回答by Babak Fakhriloo
you mean that i should bind datagridview again in Timer event handler ?
你的意思是我应该在 Timer 事件处理程序中再次绑定 datagridview 吗?