如何在x秒内刷新数据网格? [英] How do I refresh the datagrid in x seconds?

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

问题描述

大家好,



我有一个项目。简单地说;



- 数据库中有一个名为'books'的表。书籍在Web应用程序上提供。没问题,直到这里。

- 我创建了一个应该警告用户的桌面应用程序; 增加了一本新书。然后列出桌面应用程序中的所有书籍。所以我使用了datagridview和一个计时器。当一本新书被添加到数据库中时,gridview会显示整个记录。



- 问题从这里开始。当用户更改图书名称或发布年份时,在用户重新启动桌面应用程序之前,这不会显示在网格视图上。



  private   void  frmAnaGovde_Load( object  sender,EventArgs e)
{
LoadBooks();
}

public void LoadBooks()
{
dataGridView1.DataSource = _books.SelectAll();
ChangeCellContent();
}

public void ChangeCellContent()
{
dataGridView1.Columns [ 3 ]。可见= false ;
dataGridView1.Columns [ 4 ]。可见= false ;
dataGridView1.Columns [ 7 ]。Visible = false ;
dataGridView1.Columns [ 8 ]。可见= false ;
dataGridView1.Columns [ 9 ]。可见= false ;
dataGridView1.Columns [ 11 ]。可见= false ;
dataGridView1.Columns [ 12 ]。可见= false ;
for int i = 0 ; i < dataGridView1.Rows.Count; i ++)
{
dataGridView1.Rows [i] .Cells [ 0 ]。Value = UlusoyKebapApp.n2.Sabitlerim.SIPARISDURUMU(Convert.ToInt32(dataGridView1.Rows [i] .Cells [ 4 ]值));
dataGridView1.Rows [i] .Cells [ 1 ]。Value = UlusoyKebapApp.n2.Sabitlerim.ODEMESEKLI(Convert.ToInt32(dataGridView1.Rows [i] .Cells [ 7 ]。Value));
dataGridView1.Rows [i] .Cells [ 2 ]。Value = UlusoyKebapApp.n2.Sabitlerim.PAKETSEKLI(Convert.ToInt32(dataGridView1.Rows [i] .Cells [ 8 ]。Value));
}
}

public void InitTimer( )
{
Zamanlayici = new Timer();
Zamanlayici.Tick + = new EventHandler(Zamanlayici_Tick);
Zamanlayici.Interval = 2000 ; //
Zamanlayici.Start();
}
私有 void Zamanlayici_Tick( object sender,EventArgs e)
{
LoadBooks();
}



您有解决方案吗?

解决方案

我认为最适合您的方法是使用 SqlDependency进行数据更改事件 [ ^ ]。

Hello everyone,

I have a project. Simply;

- There is a table in database named 'books'. Books are served on a web application. No problem till here.
- I created a desktop application which should warn the user; "a new book has been added." and then list all the books inside the desktop application. So I used datagridview and a timer. When a new book has been added into the database, gridview shows the whole records.

- Problem starts here. When the user changes the name of a book, or publishing year, this isn't being showed on the grid view till the user restart the desktop app.

private void frmAnaGovde_Load(object sender, EventArgs e)
       {
           LoadBooks();
       }

       public void LoadBooks()
       {
           dataGridView1.DataSource = _books.SelectAll();
           ChangeCellContent();
       }

       public void ChangeCellContent()
       {
           dataGridView1.Columns[3].Visible = false;
           dataGridView1.Columns[4].Visible = false;
           dataGridView1.Columns[7].Visible = false;
           dataGridView1.Columns[8].Visible = false;
           dataGridView1.Columns[9].Visible = false;
           dataGridView1.Columns[11].Visible = false;
           dataGridView1.Columns[12].Visible = false;
           for (int i = 0; i < dataGridView1.Rows.Count; i++)
           {
               dataGridView1.Rows[i].Cells[0].Value = UlusoyKebapApp.n2.Sabitlerim.SIPARISDURUMU(Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value));
               dataGridView1.Rows[i].Cells[1].Value = UlusoyKebapApp.n2.Sabitlerim.ODEMESEKLI(Convert.ToInt32(dataGridView1.Rows[i].Cells[7].Value));
               dataGridView1.Rows[i].Cells[2].Value = UlusoyKebapApp.n2.Sabitlerim.PAKETSEKLI(Convert.ToInt32(dataGridView1.Rows[i].Cells[8].Value));
           }
       }

       public void InitTimer()
       {
           Zamanlayici = new Timer();
           Zamanlayici.Tick += new EventHandler(Zamanlayici_Tick);
           Zamanlayici.Interval = 2000;//
           Zamanlayici.Start();
       }
       private void Zamanlayici_Tick(object sender, EventArgs e)
       {
           LoadBooks();
       }


Do you have any solutions?

解决方案

I believe the best option for you is to use SqlDependency for data change events[^].


这篇关于如何在x秒内刷新数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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