原帖:http://bbs.bccn.net/thread-341646-1-1.html

最近一直在研究调度 涉及到用调度实时给客户发送短信的功能 其中就有用到实时更新的显示发送状态的 当然 今天不是以QUARTE为主

主要控件有 datagridview checkbox picturebox trackBar1 label
datagridview
:实时显示数据
checkbox :指示是否停止更新
picturebox :显示更新状态
trackBar1
:设置更新时间频率
label :显示一些相关信息
有时候我们希望能够实时的去更新一些信息 大家可能会想到Timer 但是这样做会使界面很卡
影响效果和交互性 怎样才能让它不卡又能实时更新呢
线程
主要代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WinMilkProject.Project
{public partial class Form1 : Form{Thread myThread;public int frequency = 0;//更新时间频率public static bool isUse = false;//是否停止更新public static string statusInfo = string.Empty;//状态private delegate void myDelegate(DataTable dt);//定义委托public Form1(){InitializeComponent();label2.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒";}private void Form1_Load(object sender, EventArgs e){myThread = new Thread(startFillDv);//实例化线程
            myThread.Start();}private void startFillDv(){while (true){if (isUse){statusInfo = "正在实时更新数据......";      DataTable dt = CommonEx.GetDataTableEx("select * from table1");//自己写的数据封装类 能够返回一个datatable
                    Grid(dt);Thread.Sleep(frequency);}else{statusInfo = "停止更新!";}}}private void Grid(DataTable dt){if (this.InvokeRequired){this.Invoke(new myDelegate(Grid), new object[] { dt });}else{try{this.dataGridView1.DataSource = null;this.dataGridView1.DataSource = dt;dt = null;statusInfo = "更新完成!";}catch {}}}private void Form1_FormClosed(object sender, FormClosedEventArgs e){if (this.myThread.IsAlive){this.myThread.Abort();//结束线程
            }}private void timer1_Tick(object sender, EventArgs e){label1.Text = statusInfo;frequency = trackBar1.Value;if (statusInfo.Trim() == "正在实时更新数据......"){pictureBox1.Visible = true;}else{pictureBox1.Visible = false;}}private void checkBox1_CheckedChanged(object sender, EventArgs e){if (checkBox1.Checked){isUse = true;}else{isUse = false;}}private void trackBar1_Scroll(object sender, EventArgs e){label2.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒";}}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication1
{public partial class FormMain : Form{Test.Model.queue queue = null;Test.BLL.queue queueBLL = new Test.BLL.queue();Test.BLL.ChangeDB changeDbBLL = new Test.BLL.ChangeDB();Thread myThread;public int frequency = 0;//更新时间频率public static bool isUse = false;//是否停止更新public static string statusInfo = string.Empty;//状态// private delegate void myDelegate(DataTable dt);//定义委托private delegate void myDelegate(int id);//定义委托public FormMain(){InitializeComponent();label2.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒";this.dataGridView1.DataSource = queueBLL.GetAllList();this.dataGridView1.DataMember = "ds";}private void FormMain_Load(object sender, EventArgs e){myThread = new Thread(startFillDv);//实例化线程
            myThread.Start();}private void startFillDv(){while (true){if (isUse){//statusInfo = "正在实时更新数据......";//DataTable dt = queueBLL.GetAllList().Tables[0];//自己写的数据封装类 能够返回一个datatable
                    Grid(Test.BLL.ModifStatic.Id);Thread.Sleep(frequency);}else{//statusInfo = "停止更新!";
                }}}private void Grid(int id){if (this.InvokeRequired){this.Invoke(new myDelegate(Grid), new object[] { id });}else{try{//修改改id对应的行for (int i = 0; i < this.dataGridView1.Rows.Count; i++){if (Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value) == id){queue = queueBLL.GetModel(id);this.dataGridView1.Rows[i].Cells[1].Value = queue.remainNum;}}// statusInfo = "更新完成!";
                }catch{}}}private void FormMain_FormClosed(object sender, FormClosedEventArgs e){if (this.myThread.IsAlive){this.myThread.Abort();//结束线程
            }}private void checkBox1_CheckedChanged(object sender, EventArgs e){if (checkBox1.Checked){isUse = true;}else{isUse = false;}}Thread th = null;private void buttonModif_Click(object sender, EventArgs e){th = new Thread(new ThreadStart(changeDbBLL.DBAdd));th.Start();}//private void Grid(DataTable dt)//{//    if (this.InvokeRequired)//    {//        this.Invoke(new myDelegate(Grid), new object[] { dt });//    }//    else//    {//        try//        {//            this.dataGridView1.DataSource = null;//            this.dataGridView1.DataSource = dt;//            this.dataGridView1.DataMember = "ds";//            dt = null;//            statusInfo = "更新完成!";//        }//        catch//        {//        }//    }//}private void trackBar1_Scroll_1(object sender, EventArgs e){label2.Text = "更新频率为:" + trackBar1.Value.ToString() + "秒";}private void timer1_Tick(object sender, EventArgs e){label1.Text = statusInfo;frequency = trackBar1.Value;if (statusInfo.Trim() == "正在实时更新数据......"){pictureBox1.Visible = true;}else{pictureBox1.Visible = false;}}}}

转载于:https://www.cnblogs.com/maijin/archive/2012/12/23/2829791.html

如何实现DataGridView实时更新数据【Z】相关推荐

  1. datagridview实时更新数据_旭诺云盒|智能办公新趋势进出口数据自动提取,通关状态实时更新...

    春节期间,很多公司同事都被滞留在老家无法返回公司上班,为了保证公司业务正常运转,同事之间依靠邮件.微信.QQ等工具进行文件和数据的传递,增加了很多数据整理时间.且电子口岸.单一窗口这些进出口企业频繁使 ...

  2. datagridview实时更新数据_急报!福布斯实时数据更新马云身家656亿美元登顶中国首富...

    10月29日,蚂蚁集团正式开启申购.随着蚂蚁集团上市在即,福布斯实时更新了马云的身家.数据显示,截止到12:00,其身家已达656亿美元,再次登顶中国首富. 福布斯中国富豪榜实时数据 按马云持股阿里巴 ...

  3. echarts简单使用、echarts通过计时器进行动态更新数据、echarts连接数据库实时更新数据

    1.echarts引入 在官网下载并放入项目中. 引入echarts.js文件: <script src="./echarts.js"></script> ...

  4. [转载]Qt之模型/视图(实时更新数据)

    原文地址:Qt之模型/视图(实时更新数据)作者:一去丶二三里 上两节简单介绍了Qt中对于模型/视图的编程,大部分助手里说的很清楚了,现在就开始实战部分吧! 在实际应用中,视图展示的数据往往并非一成不变 ...

  5. [转载]Qt之模型/视图(实时更新数据)_vortex_新浪博客

    原文地址:Qt之模型/视图(实时更新数据)作者:一去丶二三里 上两节简单介绍了Qt中对于模型/视图的编程,大部分助手里说的很清楚了,现在就开始实战部分吧! 在实际应用中,视图展示的数据往往并非一成不变 ...

  6. 微信小程序使用echarts实时更新数据以及常见bug

    ** 微信小程序使用echarts实时更新数据以及常见bug ** 参考echarts官方文档:https://echarts.apache.org/zh/tutorial.html 下载小程序ech ...

  7. tkinter动态表格 - 实时更新数据(TkinterTable)

    tkinter动态表格 - 实时更新数据(TkinterTable) 参考 [TkinterTable] Tkinter上的表格GUI] https://github.com/dmnfarrell/t ...

  8. Echarts实时更新数据

    先了解一下什么是AJAX AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这 ...

  9. DataGridView多线程更新数据的问题的解决办法

    我通过INotifyPropertyChanged接口让DataGridView自动更新显示内容,但在多线程更新的时候,却发生了妙名其妙的问题,DataGridView居然抛出了这样一个异常:Bind ...

最新文章

  1. [洛谷P4721]【模板】分治 FFT
  2. 检验开发团队好不好的12个问题
  3. 【C#语言规范】从FxCop归纳出来的一些规范建议
  4. 惠普企业(HPE)是否免不了最终被关停的命?
  5. socket不能bind请求的地址_深入浅出讲解:php的socket通信
  6. CentOS7下如何正确安装并启动Docker(图文详解)
  7. python k线斜率计算公式_通达信K线斜率指标公式
  8. 《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案 第11章
  9. STM32+ADS1110
  10. 个别照片查看器无法显示此图片因为计算机上,在Windows7中打开照片,提示“Windows 照片查看器无法显示此图片,因为计算机上的可用内存可能不足。....”...
  11. qq 客服 php,QQ支持临时会话设置
  12. When you gather it, things would be counted. ​​​​
  13. 解决桌面图标或者开始菜单图标不能正确显示问题
  14. 京东APP鸿蒙版开发实践,有点牛逼哦!
  15. ES6中的for...in/of的使用
  16. C++ 类模板对象传参方式
  17. 安装libssl-dev
  18. java string 转 object_java 类型转换 Object和String互转
  19. 从json提取数据,保存成txt格式
  20. 【模型库】日本数控铣床RAPIMA,RMX-15 三位模型

热门文章

  1. 异常解决(二)-- AttributeError: cannot assign module before Module.__init__() call
  2. vscode最好看的主题推荐_新学期,幼儿园环创主题墙及楼道,这样布置最好看!...
  3. Vitalik Buterin:Casper 权益证明与分片技术最新进展
  4. 直击Titan图数据库:如何提升25%+的反欺诈检测效率?
  5. 区块链架构、跨链和演进
  6. Android Binder 分析——数据传递者(Parcel)
  7. Android常用开源库之Universal-image-loader
  8. JZOJ 4909. 【NOIP2017模拟12.3】李电下棋
  9. 统计各个函数的耗时_Prometheus 常用函数 histogram_quantile 的若干“反直觉”问题...
  10. 什么材质耐酸碱_粘玻璃用什么胶水?选择高透明强力胶水不后悔!