【问题标题】:access database not updating in DataGridView after deletion of a row删除行后访问数据库未在 DataGridView 中更新
【发布时间】:2014-07-12 01:24:17
【问题描述】:

我正在开发一个适用于计算机的应用商店,我需要一个从商店中删除产品的选项,因此我创建了一个 DataGridView数据源 其中是访问的数据库文件。

当我第一次调试时,我删除了一行并在 DataGridView 中更新并在 Access 数据库文件中更新,但是当我退出应用程序重新调试时,列表再次显示已删除的行(虽然它没有显示在访问数据库文件中)。

当我删除任何行时,它也会导致错误(SystemNullReferenceException)

我正在使用 OleDb 提供程序。

这是我的代码:

namespace CompStore
{
    public partial class ProductRemove : Form
    {
        private string str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\C#\CompStore\Store.accdb";
        OleDbConnection con;
        OleDbCommand com;


        public ProductRemove()
        {
            con = new OleDbConnection(str);
            InitializeComponent();
        }

        private void ProductRemove_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'storeDataSet.Products' table. You can move, or remove it, as needed.
            this.productsTableAdapter.Fill(this.storeDataSet.Products);

        }




        private void button1_Click_1(object sender, EventArgs e)
        {
            con.Open();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow delrow = dataGridView1.Rows[i];
                if (delrow.Selected == true)
                {

                    try
                    {
                        com.CommandText = "DELETE  FROM Products WHERE ProductID=" + dataGridView1.Rows[i].Cells[0].Value + "";
                        com.Connection = con;
                        int count = com.ExecuteNonQuery();
                    }
                    catch (Exception ex) { MessageBox.Show(ex.ToString()); }
                    dataGridView1.Rows.RemoveAt(i);
                }
            }


            con.Close();
        }


    }
}

【问题讨论】:

    标签: c# database oledb nullreferenceexception


    【解决方案1】:

    我认为这段代码可能适用于解决您的 DataGridView 问题(我自己使用这段代码):

    private void button1_Click_1(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in this.datagridview1.SelectedRows)
        {
            datagridview1.Rows.RemoveAt(item.Index);
        }
    }
    

    (写这个完整的foreach代码sn-p会比只写里面的语句要好,确保这个代码sn-p在for循环之外!)

    您可以在编写删除数据库行的代码之前编写此代码。 希望这对你也有帮助。当您(或其他任何人)尝试此代码以判断它是否有效时,请向我发送回复。

    【讨论】:

      【解决方案2】:

      我认为问题在于您在移动项目时正在删除它们。 可能有帮助的是创建一个要删除的 id 列表。然后最后将它们全部删除并重新绑定网格。

      类似这样的:

                  var idsToRemove = new List<int>();
                  var rowsToRemove = new List<int>();
                  for (int i = 0; i < dataGridView1.Rows.Count; i++)
                  {
                      DataGridViewRow delrow = dataGridView1.Rows[i];
                      if (delrow.Selected == true)
                      {
      
                          try
                          {
                              //com.CommandText = "DELETE  FROM Products WHERE ProductID=" + dataGridView1.Rows[i].Cells[0].Value + "";
                              //com.Connection = con;
                              //int count = com.ExecuteNonQuery();
                              idsToRemove.add(dataGridView1.Rows[i].Cells[0].Value);
                          }
                          catch (Exception ex) { MessageBox.Show(ex.ToString()); }
                          //dataGridView1.Rows.RemoveAt(i);
                          //^this changes the list length throws things off
                                                  rowsToRemove.Add(i);
                      }
                  }
      
                  con.Open();
                  com.CommandText = "DELETE  FROM Products WHERE ProductID in(" + string.join(',', idsToRemove.ToArray() + ")";
                  con.Close();
      
                  //now rebind your grid so it has the right data
      
                                  // or for each row to remove dataGridView1.Rows.RemoveAt(arow)
      

      希望对你有帮助

      【讨论】:

      • 如何重新绑定我的网格?
      • 您最初是如何获得其中的数据的?您也可以保留要删除的索引列表,虽然不是最干净的,但现在可以使用
      • 改变了我的回答,说要保留要删除的行列表
      • 我没有写代码绑定它,我把datagridview拖到窗体时visual studio打开了一个框,我绑定了access文件的表格
      • @GabourX,你大概可以说dataGridView1.DataBind()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多