• c# DataGirdView动态刷新


    using MySql.Data.MySqlClient;
    using System;
    using System.Data;
    using System.Threading;
    using System.Windows.Forms;

    namespace DataGirdView
    {
    public partial class Form1 : Form
    {
    Thread myThread;
    string DBConnationstring = "数据库连接";
    MySqlCommand cmd;
    MySqlConnection con;
    MySqlDataAdapter msda;
    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 = getdata("select * from orderimpoert");//自己写的数据封装类 能够返回一个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() + "秒";
    }
    public DataTable getdata(string sql)
    {

    try
    {
    con = new MySqlConnection(DBConnationstring);
    con.Open();
    cmd = new MySqlCommand(sql, con);
    cmd.CommandType = CommandType.Text;
    DataTable dt = new DataTable();
    msda = new MySqlDataAdapter(cmd);
    msda.Fill(dt);
    con.Close();
    con.Dispose();
    return dt;
    }
    catch (Exception ex)
    {
    con.Close();
    con.Dispose();
    return null;
    }
    }
    public int ExecSql(string sql)
    {
    int a = 0;
    try
    {
    con = new MySqlConnection(DBConnationstring);
    con.Open();
    cmd = new MySqlCommand(sql, con);
    a = cmd.ExecuteNonQuery();
    con.Close();
    con.Dispose();
    return a;
    }
    catch (Exception)
    {
    con.Close();
    con.Dispose();
    return -1;
    }
    }
    }
    }

    -----------------------------------------------------------------------------------------

    源码:https://files-cdn.cnblogs.com/files/Zingu/DataGirdView.rar

  • 相关阅读:
    CMMI学习系列(1)CMMI简介及证书介绍
    Lync 2010 系统架构 学习笔记(2)
    Lync 2010 标准版 AD控制器搭建 学习笔记(3)
    云计算 学习笔记(4) HDFS 简介及体系结构
    云计算 学习笔记(1) Hadoop简介
    Lync 2010 Lync客户端测试 学习笔记(7)
    Lync 2010 监控服务器配置 学习笔记(8)
    CMMI学习系列(7)组织过程库,预评估,正式评估。
    CMMI学习系列(5)CMMI3过程规范制定
    CMMI学习系列(6)项目试点
  • 原文地址:https://www.cnblogs.com/Zingu/p/11447652.html
Copyright © 2020-2023  润新知