【问题标题】:how to update or refresh datagrid view connected with Ms-access in C#如何在 C# 中更新或刷新与 Ms-access 连接的数据网格视图
【发布时间】:2012-05-07 10:27:30
【问题描述】:

嗨,我已经将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 中。任何帮助将不胜感激

【问题讨论】:

    标签: c# ms-access datagridview


    【解决方案1】:

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

    Dataset sDs = new DataSet();

    sAdapter.Fill(sDs, "T1");

    sTable = sDs.Tables["T1"];

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

    【讨论】:

      【解决方案2】:

      要在插入一些数据后更新你的数据网格视图,你应该重新绑定你的数据网格视图,一些 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() 方法

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

      【讨论】:

      • 感谢 khashayar,但我认为这适用于与 microsoft sql 的连接,而不适用于 Ms-access,您能否使用 oleDbcommand 向我展示这一点。最好的问候
      • @abelo 我为 Oledb Provider 类编辑了上面的代码,如果它解决了你的问题,请标记为答案,这样其他有同样问题的人在这里找到答案!
      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      相关资源
      最近更新 更多