• 如何实现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.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;
                }
            }
    
    
    
    
    
        }
    
    }
  • 相关阅读:
    关于Python安装PIL库失败的原因
    SSH免密登录及配置完成后仍需要输入密码的解决办法
    Github+Hexo博客搭建教程(三)
    Github+Hexo博客搭建教程(二)
    Github+Hexo博客搭建教程(一)
    winform中控件的简单数据绑定
    分享一个跨线程访问控件的很实用的方法
    自定义两个控件,一个是显示图标和文字的矩形,一个是带边框的label(但是不是label)
    写一个给字符串根据长度添加换行符的处理方法
    Winform DataGridView控件在业务逻辑上的简单使用
  • 原文地址:https://www.cnblogs.com/maijin/p/2829791.html
Copyright © 2020-2023  润新知