如何实现DataGridView实时更新数据【Z】
生活随笔
收集整理的這篇文章主要介紹了
如何实现DataGridView实时更新数据【Z】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
原帖: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.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】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓学习之--如何关闭所有的activi
- 下一篇: 文件流习题8.3 8.4