1

嗨,我已经连接了数据库Ms-access 2010(.mdb)C#然后我想在datagrid视图中显示它,这是我用来保存的代码或insert the data

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 中。任何帮助将不胜感激

4

2 回答 2

2

这是插入一些数据后更新数据网格视图的最佳方法。

Dataset sDs = new DataSet();

sAdapter.Fill(sDs, "T1");

sTable = sDs.Tables["T1"];

dataGridView1.DataSource = sDs.Tables["T1"];

于 2012-10-15T03:54:17.043 回答
0

要在插入一些数据后更新你的 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() 方法

如果您解决了您的问题,请标记为答案

于 2012-05-07T10:38:44.907 回答