Real-time update data is reflected in dataview or the DataGrid is in winform ·····

Source: Internet
Author: User

Write a thread:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. xml;
Using system. Threading;
Using system. Data. sqlclient;

Namespace testform
{
Public partial class form1: Form
{
// Define a variable to close the thread
Private bool flag;
// Define a thread
Private thread;

// Define a delegate
Public Delegate void myinvoke (Dataset DS );

Public form1 ()
{
Initializecomponent ();
Flag = true;
Thread = new thread (New threadstart (threadproc ));
Thread. Start ();
}

// Thread callback function
Private void threadproc ()
{
While (FLAG)
{
// Once every three seconds
Databind ();
Thread. Sleep (3000 );
}
}
// Operate the datagridview
Private void binddatagridview (Dataset DS)
{
This. Maid = Ds. Tables ["student"];
}
// Retrieve data from the database and bind it
Private void databind ()
{
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqldataadapter SDA = new sqldataadapter ("select * From studentinfor", con );
Dataset DS = new dataset ();
SDA. Fill (DS, "student ");
// This. Maid = Ds. Tables ["student"];
// Bind data to the datagridview
Myinvoke MI = new myinvoke (binddatagridview );
This. begininvoke (MI, new object [] {DS });
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}

Private void button#click (Object sender, eventargs E)
{
// Test and add a piece of data to check whether the data can be retrieved after a period of time ..
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqlcommand cmd = new sqlcommand ("insert into studentinforvalues ('2013', 'test')", con );
Cmd. executenonquery ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}

Private void form=formclosing (Object sender, formclosingeventargs E)
{
// Close the thread before closing
Flag = false;
Thread. Join ();
}

}
}

// ================================================
Use timer to write one:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. sqlclient;
Using system. Threading;

Namespace Test
{
Public partial class form1: Form
{

Public form1 ()
{
Initializecomponent ();
// Set the interval to start Timer
This. timer1.interval = 3000;
This. timer1.enabled = true;

}

// Retrieve data from the database and bind it
Private void databind ()
{
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqldataadapter SDA = new sqldataadapter ("select * From studentinfor", con );
Dataset DS = new dataset ();
SDA. Fill (DS, "student ");
This. Maid = Ds. Tables ["student"];
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}


Private void button#click (Object sender, eventargs E)
{
// Test and add a piece of data to check whether the data can be retrieved after a period of time ..
Sqlconnection con = new sqlconnection ("Server =.; database = student; uid = sa; Pwd = 0421 ");
Try
{
Con. open ();
Sqlcommand cmd = new sqlcommand ("insert into studentinfor values ('20140901', 'test')", con );
Cmd. executenonquery ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Con. Close ();
}
}

Private void timereffectick (Object sender, eventargs E)
{
// Respond every three seconds
Databind ();
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.