You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLite中DataGridView行删除失败问题求助

删除操作异常问题求助
  • 问题一:确认删除前,DataGridView中的行已被视觉上删除
  • 问题二:数据库中的条目未被删除,重启程序后数据全部恢复
  • 问题三:删除第一行后,再删除新行时不会弹出确认消息框

相关代码如下:

private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    foreach (DataGridViewRow item in dataGridView1.Rows)
    {
        if (MessageBox.Show("Are you sure you want to delete these data? ", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
        {
            SQLiteConnection dbConnection = new SQLiteConnection("Data Source=" + dataSource + ";Version=3;"); //Create the connection with database
            dbConnection.Open();
            SQLiteCommand cmd = new SQLiteCommand("Delete From druckerliste Where id='" + item.Cells[1].Value.ToString() + "'", dbConnection);
            cmd.ExecuteNonQuery();
            dbConnection.Close();
            MessageBox.Show("Success");
        }
    }    
}

我尝试过以下调整:

  • 插入判断代码if (bool.Parse(item.Cells[0].Value.ToString()))时抛出异常
  • 修改为if (Convert.ToBoolean(Convert.ToInt32("0")))后,首次操作无消息框弹出,也无成功提示

编辑补充:
将SQL语句中的item.Cells[1]改为item.Cells[0]:

SQLiteCommand cmd = new SQLiteCommand("Delete From druckerliste Where id='" + item.Cells[0].Value.ToString() + "'", dbConnection);

但问题变为每次都会删除DataGridView中的第一个条目。

内容的提问来源于stack exchange,提问作者TheQuestionmark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 14:54:19