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
c# DataGirdView动态刷新的更多相关文章
- listview(3、动态刷新)
listview的动态刷新主要是调用adapter的notifyDataSetChanged. 在下面的例子中除了记录正常的刷新外,还记录一种错误的情况(注释掉的),作为备忘. notifyDataS ...
- jquery动态刷新select的值,后台传过来List<T>,前台解析后填充到select的option中
jquery动态刷新select的值:将后台传来的List<T>赋值到select下的option. 第一个select选择后出发该方法refreshMerchant(params),传递 ...
- listivew 动态刷新单个item
使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends ...
- ListView的动态刷新问题——用notifyDataSetChanged没作用
也许很多开发的朋友,尤其是Android初学者(笔者也是个初学者),在动态刷新ListView时,使用notifyDataSetChanged并没有起到作用.有时会被困扰得很痛苦. 其实,在使用not ...
- Android ListView内容变化后的动态刷新
ListView内容变化后的动态刷新 基本知识点: 1.更新适配器Adapter数据源 2.调用适配器Adapter的刷新方法notifyDataSetChanged() 首先需要定义ListView ...
- iOS-Storyboad动态刷新
iOS-Storyboad动态刷新 什么叫做Storyboard动态刷新 在项目开发中,如果可以在xib(storyboard)中,动态显示运行效果图,那么实在是太爽了.在Xcode 6之后就为我们提 ...
- ListView动态刷新adapter.notifyDataSetChanged()无反应
前段时间在做一个动态刷新ListView(模拟聊天),遇到一个问题,调用notifyDataSetChanged()方法,数据源已经存在但是并没有动态刷新! 首先我们需要了解notifyDataSet ...
- WinForm的DataGirdView判断CheckBox是否被选中
首先我们先设置下DataGirdView的列. 然后启动下编辑,就可以选中与不选中了.在之后通过. #region 便利被选中的行,然后导出 DataTable dtreport = new Data ...
- Apollo配置中心动态刷新日志级别
Apollo配置中心动态刷新日志级别 添加次配置后,当在apollo上面调整日志级别不需要重启服务器,马上就能生效 /** * 结合apollo动态刷新日志级别 * @author: nj * @da ...
随机推荐
- WMI在渗透测试中的重要性
0x01 什么是wmi WMI可以描述为一组管理Windows系统的方法和功能.我们可以把它当作API来与Windows系统进行相互交流.WMI在渗透测试中的价值在于它不需要下载和安装, 因为WMI是 ...
- CF 1477A. Nezzar and Board
传送门 思路: 从k = 2 * x - y ==> 2 * x = k + y ,可以看出x是k,y的中间值,则如果存在x1,x2,且x1 = x2 ± 1,则通过x1,x2可以得到所有整数, ...
- Sentry 中文版
Sentry 中文版 汉化 https://sentry.io/settings/account/details/ 打开用户设置 User settings 语言选择中文 Simplified Chi ...
- React.createClass vs. ES6 Class Components
1 1 1 https://www.fullstackreact.com/articles/react-create-class-vs-es6-class-components/ React.crea ...
- how to using js to realize notes feature on the website
how to using js to realize notes feature on the website js & notes demos https://medium.com/brow ...
- Taro 版本
Taro 版本 https://taro-docs.jd.com/taro/versions.html 1.x 1.3.34 https://taro-docs.jd.com/taro/docs/1. ...
- 不同浏览器CSS样式不兼容问题
一句话,我想的太复杂了.向朋友请教才了解到,其实只要加个判断即可,首先获取到浏览器的基本信息,像什么版本啊,名称啊.默认语言啊等等,然后根据不同浏览器默认加载不同CSS样式即可,获取浏览器版本的连接如 ...
- ElasticSearcher的安装以及安装过程中出现的问题
先给出参考链接,带安装成功后再进行总结整个过程. 参考链接:https://blog.csdn.net/fjyab/article/details/81101284 java操作ElasticSear ...
- Java 搭建 RabbitMq 消息中间件
前言 当系统中出现"生产"和"消费"的速度或稳定性等因素不一致的时候,就需要消息队列. 名词 exchange: 交换机 routingkey: 路由key q ...
- Portainer中文汉化
一.概述 Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swar ...