向access数据库中插入一条语句,DataGridView不能及时刷新
遇到一个问题,我向access数据库中插入一条语句,然后自定义函数重绑数据源刷新DataGridView,但发现不能及时刷新,一定要睡1秒,等插入语句执行完毕后才能正常刷新。我纳闷,插入一条记录要那么久?!mycom=new OleDbCommand("Insert into sites (sitename,siteurl,sitekey,sitecode) values ('"+textBox1.Text+"','"+textBox2.Text+"','"+comboBox1.SelectedItem.ToString()+"','"+textBox3.Text+"')",myOleDbConnection);
myOleDbConnection.Open();
mycom.ExecuteNonQuery();
System.Threading.Thread.Sleep(1000);
fm.RefreshGrid();
RefreshGrid代码大致如下:
public void RefreshGrid()
{
if (this.InvokeRequired)
{
Mydelegate mydel1 = RefreshGrid;
this.Invoke(mydel1);
}
else
{
//dataGridView1.Refresh();这样刷新无效,因为数据库集还是老集
// String sqlstr = "select * from sites";
ds = new DataSet(); //不写会保留原来记录再添加一遍,也不能写ds=null
myadapter.Fill(ds, "Table1");
dataGridView1.DataSource = null;
dataGridView1.DataSource = ds.Tables["Table1"];
// dataGridView1.Refresh();
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[3].Visible = false;
dataGridView1.Columns[5].Visible = false; //得重新设定,不然会恢复所有行都可见
dataGridView1.Columns[2].Width = 200;
}
}