在C#中刷新数据网格视图 [英] refresh a datagrid view in C#

查看:140
本文介绍了在C#中刷新数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows窗体中将datagrid视图进行了数据绑定. &我也增加了一个计时器.因此,在timer_tick方法中,我调用了一种从databse将数据检索到gridview的方法.但是现在我想在更新表格后立即刷新datagrid视图.我添加了以下代码,但是它不起作用.只有它添加了数据库中的值.

I have databinded a datagrid view in windows form. & also i have added a timer as well. So in the timer_tick method, i had called a method to retrieve data to the gridview from databse. But now i want to refresh the datagrid view as soon as it updates the table. I have added following code, but it doesn''t work. Only it added the values from the database.

private void timer1_Tick(object sender, EventArgs e)
        {
            
            this.dataGridView1.Refresh();
            this.dataGridView1.Parent.Refresh();
   

        }

推荐答案

请参见以下代码,希望对您有所帮助:

See the following code, hope it helps you:

protected void Timer1_Tick(object sender, EventArgs e) 
         {                  
                 FillDataGridView(); 
         } 
 
 
         protected void FillDataGridView() 
         { 
                 DataSet objDs = new DataSet(); 
                 SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString); 
                 SqlDataAdapter myCommand; 
                 string select = "SELECT * FROM Table1"; 
                 myCommand = new SqlDataAdapter(select, myConnection); 
                 myCommand.SelectCommand.CommandType = CommandType.Text; 
                 myConnection.Open(); 
                 myCommand.Fill(objDs); 
                 GridView1.DataSource = objDs; 
                 GridView1.DataBind(); 
         }


this.tblCountWIPTableAdapter.Fill(this.bmaDashbordDataSet.tblCountWIP);


这篇关于在C#中刷新数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆