日期:2014-05-17  浏览次数:21035 次

DataGridView 绑定自动刷新的问题
      private void bind()
        {
            string str = "select * from department";

            {
                string connstr = frmMain.GetConnString();
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                da = new SqlDataAdapter(str, conn);
                da.Fill(ds);
                while (dataGridViewX1.Rows.Count - 1 > 0)
                {
                    dataGridViewX1.Rows.Remove(dataGridViewX1.Rows[0]);
                }

                if (dataGridViewX1.Columns.Contains("deptid"))
                {
                    dataGridViewX1.Columns.Remove("deptid");
                    dataGridViewX1.Columns.Remove("deptname");
                    
                }

                dataGridViewX1.Visible = true;
                dataGridViewX1.Columns.Clear();
                dataGridViewX1.Columns.Add("deptid", "办事机构ID");
                dataGridViewX1.Columns["deptid"].DataPropertyName = "deptid";
                dataGridViewX1.Columns.Add("deptname", "办事机构名称");
                dataGridViewX1.Columns["deptname"].DataPropertyName = "deptname";
                dataGridViewX1.DataSource = ds.Tables[0];
                dataGridViewX1.Refresh();
                conn.Close();
            }