C# 如何在C中更新或刷新与Ms access连接的datagrid视图#
您好,我已经将C# 如何在C中更新或刷新与Ms access连接的datagrid视图#,c#,ms-access,datagridview,C#,Ms Access,Datagridview,您好,我已经将Ms access 2010(.mdb)中的数据库与C连接,然后我想在datagrid视图中显示它,下面是我用来保存或插入数据的代码 OleDbCommand cmd = new OleDbCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into VMS(PlateNo,JobCardNo,Model,DateIn,Status,PartList,Pur
Ms access 2010(.mdb)
中的数据库与C
连接,然后我想在datagrid
视图中显示它,下面是我用来保存或插入数据的代码
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into VMS(PlateNo,JobCardNo,Model,DateIn,Status,PartList,PurchNo,PurchDate,Remark)" + "values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "')";
cmd.Connection = mycon;
mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();
这是正确的,问题是当我保存新数据时,它会保存在MS-access中,但它不会更新或显示在datagridview中。如果您想在插入一些数据后更新您的datagridview,请提供任何帮助,我们将不胜感激。您应该重新绑定数据网格视图,一些sudocode可能如下所示:
OledbConnection conn=new OledbConnection("your connectionstring");
OledbCommand comm=new OledbCommand("SELECT * FROM yourtablename",conn);
OledbDataAdapter da=new OledbDataAdatpter(comm);
Dataset ds=new dataSet();
conn.open();
da.Fill(ds,"T1");
datagridView.DataMember="T1";
datagridview.DataSource=ds;
conn.close();
注意:如果在分配数据源后您的项目是asp.net项目,则必须以以下方式将方法DataBind()写入datagridview:
datagridview.DataSource=ds;
datagridview.DataBind();
但是在windows应用程序中,您不必执行DataBind()方法
如果您的问题已经解决,请将其标记为“答案”这是插入一些数据后更新datagridview的最佳方法
Dataset sDs=新数据集()代码>
sAdapter.Fill(sDs,“T1”)代码>
sTable=sDs.Tables[“T1”]代码>
dataGridView1.DataSource=sDs.Tables[“T1”]代码>谢谢khashayar,但我认为这适用于与microsoft sql的连接,不适用于Ms access。请使用oleDbcommand向我展示一下。最好的regards@abelo我为Oledb Provider类编辑了上面的代码,如果它解决了您的问题,请将其标记为答案,这样其他有相同问题的人可以在这里找到答案!