mfc datagrid怎么实时更新数据库内容

mfc datagrid怎么实时更新数据库内容,第1张

主要控件有 datagridview checkbox picturebox trackBar1 label

_atagridview :实时显示数据

_heckbox :指示是否停止更新

_icturebox :显示更新状态

_rackBar1 :设置更新时间频率

_abel :显示一些相关信息

?

_

?1 using System

?2 using System.Collections.Generic

?3 using System.ComponentModel

?4 using System.Data

?5 using System.Drawing

?6 using System.Text

?7 using System.Windows.Forms

?8 using System.Threading

?9

?10 namespace WinMilkProject.Project

?11 {

?12

?13 public partial class Form1 : Form

?14 {

?15 Thread myThread

?16 OperateCB operatedb = new OperateCB()

?17 public int frequency = 0//更新时间频率

?18 public static bool isUse = false//是否停止更新

?19 public static string statusInfo = string.Empty//状态

?20 private delegate void myDelegate(DataTable dt)//定义委托

?21 public Form1()

?22 {

?23 InitializeComponent()

?24 label2.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒"

?25 }

?26

?27 private void Form1_Load(object sender, EventArgs e)

?28 {

?29 myThread = new Thread(startFillDv)//实例化线程

?30 myThread.Start()

?31

?32 }

?33

?34 private void startFillDv()

?35 {

?36 while (true)

?37 {

?38 if (isUse)

?39 {

?40 statusInfo = "正在实时更新数据......"

?41 DataTable dt = operatedb.MyDataTable("select * from test1")//把自己写的数据封装类OperaterCB 能够返回一个datatable

?42 Grid(dt)

?43 Thread.Sleep(frequency)

?44 }

?45 else

?46 {

?47 statusInfo = "停止更新!"

?48 }

?49 }

?50

?51 }

?52

?53 private void Grid(DataTable dt)

?54 {

?55 if (this.InvokeRequired)

?56 {

?57 this.Invoke(new myDelegate(Grid), new object[] { dt })

?58 }

?59 else

?60 {

?61 try

?62 {

?63 this.dataGridView1.DataSource = null

?64 this.dataGridView1.DataSource = dt

?65 dt = null

?66 statusInfo = "更新完成!"

?67 }

?68 catch

?69 {

?70

?71 }

?72 }

?73

?74 }

?75

?76 private void Form1_FormClosed(object sender, FormClosedEventArgs e)

?77 {

?78 if (this.myThread.IsAlive)

?79 {

?80 this.myThread.Abort()//结束线程

?81 }

?82 }

?83

?84 private void timer1_Tick(object sender, EventArgs e)

?85 {

?86 label1.Text = statusInfo

?87 frequency = trackBar1.Value

?88 if (statusInfo.Trim() == "正在实时更新数据......")

?89 {

?90 pictureBox1.Visible = true

?91 }

?92 else

?93 {

?94 pictureBox1.Visible = false

?95 }

?96

?97 }

?98

?99 private void checkBox1_CheckedChanged(object sender, EventArgs e)

?100 {

?101 if (checkBox1.Checked)

?102 {

?103 isUse = true

?104 }

?105 else

?106 {

?107 isUse = false

?108 }

?109

?110 }

?111

?112 private void trackBar1_Scroll(object sender, EventArgs e)

?113 {

?114 label2.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒"

?115 }

?116

?117 }

?118 }

_

网上说的是对的,MFC里是没有Datagridview控件,只有listctr控件

工具箱如下图:

把listctr拖到界面上:

属性设置成: report

直接用下面代码:

SqlConnection cnn=new SqlConnection()

cnn.ConnectionString="Server=192.168.1.2uid=sapwd=123database=数据库名"

cnn.Open()

DataSet ds=new DataSet()

String sql="Select ...."

SqlDataAdapter sda=new SqlDataAdapter(sql,cnn)

sda.Fill(ds)

dataGridView1.DataSource=ds.Tables[0]

上面是最简单的连接方式。当然你如果你多次用到数据库对象,你一开始就应该static一个SqlConnection对象,让他处于打开状态。后面你要做数据处理就是修改SQL语句的事情,完全可以把它作为变量,放在类中使用,DataSet对象可以作为公共变量,当方法执行带SQL参数时他就会跟着改变数据集合,这样你就可以不像上面一样每次都去连接,浪费数据查询时间。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9664893.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存