Home > Back-end >  Can't delete row from dataGridView in SQlite
Can't delete row from dataGridView in SQlite

Time:08-18

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

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

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

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])

but the problem is, that always the first entry of the dataGridView will be deleted.

CodePudding user response:

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.

Second fault was to not change the SelectionMode to 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();
            }
        }
    }
}
  • Related