c#定时读取数据库显示在界面上,然后会很快的一闪一闪的,怎么消除这种闪烁呢,求大神帮帮忙啊。代码如下
privatevoidpTimer_Elapsed(objectsender,System.Timers.ElapsedEventArgse){dingshi();}pr...
private void pTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
dingshi();
}
private void dingshi() //这个函数就是为了定时读取数据内容
{
if (i == 1)
{
DataTable dt = new DataTable();
DataSet dataset = new DataSet();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader read = cmd.ExecuteReader();
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Update();
dt.Load(read);
this.dataGridView1.DataSource = dt;
this.dataGridView1.DataSource = null;
read.Close();
dataset.Clear();
}
else
{
DataTable dt_xx = (DataTable)dataGridView1.DataSource;
dt_xx.Rows.Clear();
dataGridView1.DataSource = dt_xx;
conn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (i == 0)
{
conn.Open();
i = 1;
pTimer.Enabled = true;
}
else
{
i = 0;
pTimer.Enabled = false;
}
dingshi();
}
private void timer1_Tick(object sender, EventArgs e)
{
pTimer.Elapsed += pTimer_Elapsed;//委托,要执行的方法
pTimer.AutoReset = true;//获取该定时器自动执行
Control.CheckForIllegalCrossThreadCalls = false;//这个不太懂,有待研究
m = j.ToString();//以下三行仅仅是为了看下是不是定时刷新了
label1.Text = m;//
j++;//
}
this.dataGridView1.DataSource = dt;
这行单步调试运行会显示“索引超出范围.必须为非负值并小于集合大小.” 展开
{
dingshi();
}
private void dingshi() //这个函数就是为了定时读取数据内容
{
if (i == 1)
{
DataTable dt = new DataTable();
DataSet dataset = new DataSet();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader read = cmd.ExecuteReader();
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Update();
dt.Load(read);
this.dataGridView1.DataSource = dt;
this.dataGridView1.DataSource = null;
read.Close();
dataset.Clear();
}
else
{
DataTable dt_xx = (DataTable)dataGridView1.DataSource;
dt_xx.Rows.Clear();
dataGridView1.DataSource = dt_xx;
conn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (i == 0)
{
conn.Open();
i = 1;
pTimer.Enabled = true;
}
else
{
i = 0;
pTimer.Enabled = false;
}
dingshi();
}
private void timer1_Tick(object sender, EventArgs e)
{
pTimer.Elapsed += pTimer_Elapsed;//委托,要执行的方法
pTimer.AutoReset = true;//获取该定时器自动执行
Control.CheckForIllegalCrossThreadCalls = false;//这个不太懂,有待研究
m = j.ToString();//以下三行仅仅是为了看下是不是定时刷新了
label1.Text = m;//
j++;//
}
this.dataGridView1.DataSource = dt;
这行单步调试运行会显示“索引超出范围.必须为非负值并小于集合大小.” 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询