DataGridView刷新问题
发表在C#图书答疑
2008-04-27
是否精华
是
否
版块置顶:
是
否
您好!最近我在学习C#,在使用DataGridView控件时出现了问题,特向您请教。代码如下:
<br>
<br>//点击toolbtnAlldel按钮删除数据表中所有数据
<br>private void toolbtnAlldel_Click(object sender, EventArgs e)
<br> {
<br> //弹出提示框,Yes全部删除,No取消删除
<br> if (MessageBox.Show(this, "全部删除?", "全部删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
<br> {
<br> String ConnectionString;
<br> string strPath = Application.StartupPath + "\\NMSdbpwd.mdb";
<br> ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=liyaolinux;User id=admin;Data source=" + strPath;
<br> OleDbConnection oleconn = new OleDbConnection(ConnectionString);
<br> OleDbCommand olecmd = new OleDbCommand("delete from tb_nport", oleconn);
<br> oleconn.Open();
<br> olecmd.ExecuteNonQuery();
<br> olecmd.Dispose();
<br> oleconn.Close();
<br> }
<br>
<br> }
<br>
<br>
<br>//在点击Yes按钮后删除数据表的信息,在Activated事件中写如下代码,重新绑定
<br>private void Form1_Activated(object sender, EventArgs e)
<br> {
<br> String ConnectionString;
<br> string strPath = Application.StartupPath + "\\NMSdbpwd.mdb";
<br> ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=liyaolinux;User id=admin;Data source=" + strPath;
<br>
<br> OleDbConnection oleconn = new OleDbConnection(ConnectionString);
<br> OleDbDataAdapter oleda = new OleDbDataAdapter("select * from tb_nport order by nportname,id", oleconn);
<br> DataSet ds = new DataSet();
<br> oleda.Fill(ds, "tbnport");
<br> this.gv.DataSource = ds.Tables[0].DefaultView;
<br> oleconn.Close();
<br>
<br> }
<br>
<br>我按一次Yes按钮Datagridview没刷新,按两次Yes按钮才刷新。我认为是DataSet的问题,请您指导。
<br>谢谢!
<br>
<br>//点击toolbtnAlldel按钮删除数据表中所有数据
<br>private void toolbtnAlldel_Click(object sender, EventArgs e)
<br> {
<br> //弹出提示框,Yes全部删除,No取消删除
<br> if (MessageBox.Show(this, "全部删除?", "全部删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
<br> {
<br> String ConnectionString;
<br> string strPath = Application.StartupPath + "\\NMSdbpwd.mdb";
<br> ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=liyaolinux;User id=admin;Data source=" + strPath;
<br> OleDbConnection oleconn = new OleDbConnection(ConnectionString);
<br> OleDbCommand olecmd = new OleDbCommand("delete from tb_nport", oleconn);
<br> oleconn.Open();
<br> olecmd.ExecuteNonQuery();
<br> olecmd.Dispose();
<br> oleconn.Close();
<br> }
<br>
<br> }
<br>
<br>
<br>//在点击Yes按钮后删除数据表的信息,在Activated事件中写如下代码,重新绑定
<br>private void Form1_Activated(object sender, EventArgs e)
<br> {
<br> String ConnectionString;
<br> string strPath = Application.StartupPath + "\\NMSdbpwd.mdb";
<br> ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=liyaolinux;User id=admin;Data source=" + strPath;
<br>
<br> OleDbConnection oleconn = new OleDbConnection(ConnectionString);
<br> OleDbDataAdapter oleda = new OleDbDataAdapter("select * from tb_nport order by nportname,id", oleconn);
<br> DataSet ds = new DataSet();
<br> oleda.Fill(ds, "tbnport");
<br> this.gv.DataSource = ds.Tables[0].DefaultView;
<br> oleconn.Close();
<br>
<br> }
<br>
<br>我按一次Yes按钮Datagridview没刷新,按两次Yes按钮才刷新。我认为是DataSet的问题,请您指导。
<br>谢谢!