删除一行后,在DataGridView中访问数据库不更新

本文关键字:DataGridView 访问 数据库 更新 一行 删除 | 更新日期: 2023-09-27 18:04:37

我正在开发一个计算机应用程序商店,我需要一个选项来删除一个产品从商店,所以我创建了一个DataGridView数据源它是访问的数据库文件。

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

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

我正在使用OleDb provider

下面是我的代码:
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();
        }

    }
}

删除一行后,在DataGridView中访问数据库不更新

我认为问题在于您在移动它们时正在删除它们。创建一个要删除的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问题(我自己使用这段代码):

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

(编写完整的foreach代码片段将比只在其中编写语句更好,确保该代码片段在for循环之外!)

您可以在编写删除数据库行代码之前编写此代码。希望这对你也有帮助。当你(或其他任何人)尝试这个代码告诉我是否工作时,给我一个回应。