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
相关产品推荐
相关产品推荐

