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

c# DataGirdView动态刷新的更多相关文章

  1. listview(3、动态刷新)

    listview的动态刷新主要是调用adapter的notifyDataSetChanged. 在下面的例子中除了记录正常的刷新外,还记录一种错误的情况(注释掉的),作为备忘. notifyDataS ...

  2. jquery动态刷新select的值,后台传过来List<T>,前台解析后填充到select的option中

    jquery动态刷新select的值:将后台传来的List<T>赋值到select下的option. 第一个select选择后出发该方法refreshMerchant(params),传递 ...

  3. listivew 动态刷新单个item

    使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends  ...

  4. ListView的动态刷新问题——用notifyDataSetChanged没作用

    也许很多开发的朋友,尤其是Android初学者(笔者也是个初学者),在动态刷新ListView时,使用notifyDataSetChanged并没有起到作用.有时会被困扰得很痛苦. 其实,在使用not ...

  5. Android ListView内容变化后的动态刷新

    ListView内容变化后的动态刷新 基本知识点: 1.更新适配器Adapter数据源 2.调用适配器Adapter的刷新方法notifyDataSetChanged() 首先需要定义ListView ...

  6. iOS-Storyboad动态刷新

    iOS-Storyboad动态刷新 什么叫做Storyboard动态刷新 在项目开发中,如果可以在xib(storyboard)中,动态显示运行效果图,那么实在是太爽了.在Xcode 6之后就为我们提 ...

  7. ListView动态刷新adapter.notifyDataSetChanged()无反应

    前段时间在做一个动态刷新ListView(模拟聊天),遇到一个问题,调用notifyDataSetChanged()方法,数据源已经存在但是并没有动态刷新! 首先我们需要了解notifyDataSet ...

  8. WinForm的DataGirdView判断CheckBox是否被选中

    首先我们先设置下DataGirdView的列. 然后启动下编辑,就可以选中与不选中了.在之后通过. #region 便利被选中的行,然后导出 DataTable dtreport = new Data ...

  9. Apollo配置中心动态刷新日志级别

    Apollo配置中心动态刷新日志级别 添加次配置后,当在apollo上面调整日志级别不需要重启服务器,马上就能生效 /** * 结合apollo动态刷新日志级别 * @author: nj * @da ...

随机推荐

  1. Gym102361A Angle Beats(直角三角形 计算几何)题解

    题意: \(n\)个点,\(q\)个询问,每次问包含询问点的直角三角形有几个 思路: 代码: #include<bits/stdc++.h> using namespace std; co ...

  2. 基金术语 All In One

    基金术语 All In One GP.LP.PE.VC.FOF LP 有限合伙人(Limited Partner, LP):我们可以简单的理解为出资人. 很多时候,一个项目需要投资上千万乃至数个亿的资 ...

  3. macOS 录屏 gif

    macOS 录屏 gif LICEcap bug 授权问题? 如何在 Mac 上录制屏幕 https://support.apple.com/zh-cn/HT208721 Command + Shif ...

  4. Proxifier

    Proxifier 使用教程 https://www.proxifier.com/ Proxifier允许不支持通过代理服务器工作的网络应用程序通过SOCKS或HTTPS代理和链进行操作. confi ...

  5. docs search & algolia & docsearch

    docs search & algolia & docsearch https://www.algolia.com/docsearch https://www.algolia.com/ ...

  6. Dart Web

    Dart Web Dart for Web https://dart.dev/platforms dart2js xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允 ...

  7. Taro 物料市场

    Taro 物料市场 taro component demo https://taro-ext.jd.com/ https://taro-ext.jd.com/plugin/view/5caab6c68 ...

  8. QR code & auto login & OAuth 2.0

    QR code & auto login & OAuth 2.0 扫码自动登录原理解析 https://www.ximalaya.com/shangye/14685881/958686 ...

  9. vue render html string

    vue render html string shit element ui render string array relativeShowConvert(data) { // log(`data` ...

  10. push notifications

    push notifications https://developers.google.com/web/fundamentals/push-notifications/ Push API https ...