zoukankan      html  css  js  c++  java
  • 实现DataGridView实时更新数据

    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() + "";
            }

        }
    }

  • 相关阅读:
    通过Maven简单搭建SSM框架
    javaWeb常用面试题
    到底什么是对象,什么是对象的引用?对象和对象的引用有那些区别?
    第二章 python中重要的数据结构(下)
    第一章 python中重要的数据结构(上)
    springboot 集成完整的swagger2
    JAVA -> 数据加密和解密 留存
    mac rar文件解压缩
    java 图片合成文字或者awt包下的对话框引入自定义字体库
    java中list或数组中随机子集工具类
  • 原文地址:https://www.cnblogs.com/ArRan/p/3333517.html
Copyright © 2011-2022 走看看