簡體   English   中英

如何在C#上實時獲取刷新的DataGrid?

[英]How to get refreshed datagrid realtime on C#?

如何在網格上自動選擇? 我創建了計時器,但計時器不保存網格選擇。 例如,當我選擇網格的第三個索引時,然后在5秒后,它選擇了第一個網格索引。 所以我在這里需要其他解決方案。

private void formList_Load(object sender, EventArgs e)
{                
     BindingSource bs = new BindingSource();       
     DataTable _dt =_myFunction.Select_New_Inserted_Info(_lDataParameter).Tables[0];

     bs.DataSource = _dt;
     gridControl1.DataSource = bs;

     timer1.Interval = 5000;
     timer1.Start();
}
 private void timer1_Tick(object sender, EventArgs e)
        {                  
      BindingSource bs = new BindingSource();       
         DataTable _dt =_myFunction.Select_New_Inserted_Info(_lDataParameter).Tables[0];

         bs.DataSource = _dt;
         gridControl1.DataSource = bs; 
        }
  • 如果您需要dataGrid(和其他綁定控件)來自動選擇活動的Binding源的記錄,請使用: bs.Current;
  • 如果要每隔x時間填充一次數據,請將代碼放入System.Windows.Forms.Timer的' Tick '事件中。

編輯

選擇指針由BindingSource處理。 因此,您必須將BindingSource放在函數之外。您的代碼將如下所示:

        BindingSource bs = new BindingSource();

        private DataTable GetDataTable()
        {
           //Please consider checking the populating data function from errors, or post your code to help you with. 
            DataTable dt =_myFunction.Select_New_Inserted_Info(_lDataParameter).Tables[0];

            return dt;
        }

        private void formList_Load(object sender, EventArgs e)
        {
            DataTable _dt = GetDataTable();

            bs.DataSource = _dt;
            gridControl1.DataSource = bs;

            timer1.Interval = 5000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DataTable _dt = GetDataTable();

            bs.DataSource = _dt;
            gridControl1.DataSource = bs;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM