简体   繁体   中英

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

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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