我的datagridview的刷新代码如下,思路是每30秒刷新一次,刷新是重新绑定数据源,还有重写了Datagridview的OnPaint函数,防止出现红叉的问题(网上查的)System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.AutoReset = true;
t.Enabled = true;public void t_Elapsed(object source, System.Timers.ElapsedEventArgs e)
        {
            this.lblSec.Text = refresh.ToString() + "秒后刷新";
            if (refresh >= 1)
            {
                refresh--;
            }
            else
            {
                if (this.cbbks.SelectedIndex >= 0)
                {
                    if (this.ccbDoc.SelectedIndex >= 0)
                    {
                        string doc = ccbDoc.SelectedItem.ToString().Trim();
                        display(cur_zq_type, doc);
                    }
                    else
                    {
                        string department = cbbks.SelectedItem.ToString().Trim();
                        cbb_doctor_list(cur_zq_type, department);
                    }
                }
                refresh = 30;
            }
        }private void display(string zq_type, string doctor)
        {
            int i = 0;
            try
            {
                dGpatient.DataSource = null;
                table.Clear();
                string str_doc = "";
                string str_department = "";
                string department = this.cbbks.SelectedItem.ToString().Trim();
                if (department.Equals("全部显示"))
                {
                    str_department = "";
                }
                else
                {
                    string depID = (string)dep2name[department];
                    str_department = " and queue_type_id = '" + depID + "'";
                }                if (doctor.Equals("全部显示"))
                {
                    str_doc = "";
                }
                else
                {
                    str_doc = " and doctor_name = '" + doctor + "' ";
                }
                string str = show + str_doc + str_department +
                    "order by (case state_call when '3' then '0' else '1' end)," +
                    "(case state_call when '6' then '1' else '2' end) ,state_call asc," +
                    " is_call asc, state_triage desc, fre_date asc";
                i = 1;
                mysqlDa = new MySqlDataAdapter(str, mysqlConn);
                i = 2;
                mysqlDa.Fill(table);
                i = 3;
                dGpatient.DataSource = table;                  this.dGpatient.Columns["状态"].Visible = false;
                i = 4                //转到刷新前的选择行
                if (cur_index >= 0 && cur_index <= dGpatient.Rows.Count - 1)
                {
                    //选中状态
                    dGpatient.Rows[cur_index].Selected = true;
                    //滚动到当前行
                    dGpatient.CurrentCell = dGpatient.Rows[cur_index].Cells[0];
                }
                i = 5;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "\n" + i.ToString());
            }
            finally
            {
                mysqlDa.Dispose();
            }
        }重写的OnPaint代码public class DataGridViewPlus : DataGridView
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {
                Invalidate();
            }        }
    }运行时,跑着跑着就在自动刷新时报错了
由于数据并不多,所以不考虑增量等方法,就以我现在重新绑定的方法下,请大侠们指教传图很慢手打
System.InvaidOperationException:This operation cannot be performed while an auto-filled column is being resized
at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFilling respositionEditionControl)....
...
3
(3是我设置的错误断点提示,还有,报错时候Datagirdview刷新出来了第一个列表头,然后出错了)
很郁闷,倒腾很久了没搞定datagridview定时刷新 报错 

解决方案 »

  1.   

    http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/b66520b7-94e4-4b13-ab82-64f3aeee23e4/另外
    dGpatient.DataSource = null;
    dGpatient.DataSource = table;
    this.dGpatient.Columns["状态"].Visible = false;
    这些没必要吧,你已经处理了table,他会自动刷新的啊
      

  2.   

    不这样做好像在刷新后要定位到刷新前的选定行时会报索引越界错误。
    btw,给的链接给力,在测试