Rama AbdelRahman
Rama AbdelRahman

Reputation: 87

save changes of DataGridView to database

I have a form with a DataGridView and a delete Button and I have this code for delete a row, how can make this delete saved to the database? I filled the dataGridView1 with DataSet.

if (dataGridView1.Rows.Count > 0)
{
    if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].IsNewRow != true)
    {
        dataGridView1.Rows.Remove(dataGridView1.CurrentRow);     
    }
}

Upvotes: 1

Views: 244

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39956

Suppose in your Table you have a column named id and you want to delete from database based on id. Try this:

private void btnDelete_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow item in dataGridView1.SelectedRows)
    {
       var id = item.Cells[0].Value.ToString();//You can change id and Cells[0] as your need
       //Write Delete code like this (Delete from Table where id = @id)
       dataGridView1.Rows.RemoveAt(item.Index);//Remove from dataGridView1
    }
}

Upvotes: 2

Related Questions