简体   繁体   English

如何在C#上实时获取刷新的DataGrid?

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

How to get selected automatically on grid? 如何在网格上自动选择? I created timer but timer doesn't save grid selection. 我创建了计时器,但计时器不保存网格选择。 For example when I select grid's 3rd index then 5 second later it select 1st grid index. 例如,当我选择网格的第三个索引时,然后在5秒后,它选择了第一个网格索引。 So I need other solution here. 所以我在这里需要其他解决方案。

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; 
        }
  • If you need the dataGrid (and other binded controls) to automatically select the active Binding source's record use: bs.Current; 如果您需要dataGrid(和其他绑定控件)来自动选择活动的Binding源的记录,请使用: bs.Current;
  • If you want to populate the data every x of time, put your code inside a System.Windows.Forms.Timer 's ' Tick ' event. 如果要每隔x时间填充一次数据,请将代码放入System.Windows.Forms.Timer的' Tick '事件中。

EDIT 编辑

The selection pointer is handled by BindingSource. 选择指针由BindingSource处理。 so, you have to put the BindingSource outside the function Your code will look like this: 因此,您必须将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