简体   繁体   中英

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. 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;
  • If you want to populate the data every x of time, put your code inside a System.Windows.Forms.Timer 's ' Tick ' event.

EDIT

The selection pointer is handled by BindingSource. so, you have to put the BindingSource outside the function Your code will look like this:

        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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM