save changes of DataGridView to database

后端 未结 1 502
半阙折子戏
半阙折子戏 2021-01-27 01:18

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 t

相关标签:
1条回答
  • 2021-01-27 01:55

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题