如何在C#上获得刷新DataGrid实时



如何在网格上自动选择?我创建了计时器,但是计时器不能保存网格选择。例如,当我选择网格的第三索引时,然后稍后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(和其他Bonded控件)来自动选择Active Binding源的记录使用:bs.Current;
  • 如果您想在每个x的每一个时间填充数据,请将代码放入System.Windows.Forms.Timer'S' 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;
        }

最新更新