简体   繁体   English

无法从 SQlite 中的 dataGridView 中删除行

[英]Can't delete row from dataGridView in SQlite

Could anyone please explain me, why the deletion does not work?谁能解释一下,为什么删除不起作用?

First problem: The row is visually deleted in the dataGridView, before I confirm the delete第一个问题:在dataGridView中可视化删除了行,在我确认删除之前

Second problem: The entry will not be delete in the database.第二个问题:数据库中的条目不会被删除。 After reloading the program all is back again.重新加载程序后,一切又回来了。

Third problem: If I delete a new row after "deleting" the first, no new Message Box appears.第三个问题:如果我在“删除”第一行之后删除了一个新行,则不会出现新的消息框。

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");
        }
    }    
}

I use a little bit help of this question here: c#- Can't delete multi rows in datagridview with sqlite database我在这里使用了这个问题的一点帮助: c#- Can't delete multi rows in datagridview with sqlite database

There is a line called有一条线叫

if (bool.Parse(item.Cells[0].Value.ToString()))

If I insert this, it gives me an exception, so I edit it with如果我插入它,它会给我一个例外,所以我编辑它

if (Convert.ToBoolean(Convert.ToInt32("0")))

as I could find in this answer here: Convert.ToBoolean fails with "0" value正如我在这里的答案中找到的那样: Convert.ToBoolean failed with "0" value

But with that line not even the Message Box appears the first time and no success Message.但是在该行中,甚至消息框都不会第一次出现,也没有成功消息。 So now, I dont know what's wrong.所以现在,我不知道出了什么问题。

Edit:编辑:

I've found a partial solution.我找到了部分解决方案。 I edit the line我编辑该行

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

(item.Cells[0] instead of item.Cells[1]) (item.Cells[0] 而不是 item.Cells[1])

but the problem is, that always the first entry of the dataGridView will be deleted.但问题是,总是会删除 dataGridView 的第一个条目。

if there's no message you can use a try catch statement to know about the exception.如果没有消息,您可以使用 try catch 语句来了解异常。 so we can understand and help you better.以便我们更好地理解和帮助您。

OK, I found the solution and yes, it was really silly what I did before.好的,我找到了解决方案,是的,我之前所做的真的很愚蠢。

First fault was to use the RowsRemoved-Event instead of KeyDown-Event.第一个错误是使用 RowsRemoved-Event 而不是 KeyDown-Event。

Second fault was to not change the SelectionMode to FullRowSelect第二个错误是没有将 SelectionMode 更改为 FullRowSelect

After that, I use the following code:之后,我使用以下代码:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete)
    {
        if (MessageBox.Show("Datensatz löschen?", "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int selectedIndex = dataGridView1.SelectedRows[0].Index;
                int rowID = int.Parse(dataGridView1[0, selectedIndex].Value.ToString());
                SQLiteConnection dbConnection = new SQLiteConnection("Data Source=" + dataSource + ";Version=3;");
                dbConnection.Open();
                SQLiteCommand cmd = new SQLiteCommand("DELETE FROM printerlist WHERE id='" + rowID + "'", dbConnection);
                cmd.ExecuteNonQuery();
                dbConnection.Close();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM