Delete row in DataGridView and database

时间:2016-02-12 20:21:07

标签: c# entity-framework

I'm completely new to databases and EF but I made a database with EF and have a DataGridView control on a windows form that I made by dragging my datasource to my form. After the user enters their information and hits the save button it succesfully saves their information in the database using this code

perror

But it doesn't show up in the database until I restart the program. When the user selects a row in the DataGridView and hits the delete button which has this code

public partial class bsMainPage : Form
{
    BSDATAContainer db = new BSDATAContainer();
    public bsMainPage()
    {
        InitializeComponent();
    }

    private void saveBtn_Click(object sender, EventArgs e)
    {
        BSRecords breakfastRecord = new BSRecords();
        breakfastRecord.BS = brkBS.ToString();
        breakfastRecord.Carbs = brkCarb.ToString();
        breakfastRecord.Notes = brkftNoteTxt.Text;
        breakfastRecord.Date = dateTxt.Text;
        BSRecords lunchRecord = new BSRecords();
        lunchRecord.BS = lchBS.ToString();
        lunchRecord.Carbs = lchCarb.ToString();
        lunchRecord.Notes = lnchNoteTxt.Text;
        lunchRecord.Date = dateTxt.Text;
        BSRecords dinnerRecord = new BSRecords();
        dinnerRecord.BS = dnrBS.ToString();
        dinnerRecord.Carbs = dnrCarb.ToString();
        dinnerRecord.Notes = dnnrNoteTxt.Text;
        dinnerRecord.Date = dateTxt.Text;
        db.BSRecords.Add(breakfastRecord);
        db.BSRecords.Add(lunchRecord);
        db.BSRecords.Add(dinnerRecord);
        db.SaveChanges();
   }
}

It deletes the data in the DataGridView but doesn't save the changes in my database. I have followed all the answers I found on here and other sites to delete in the database but nothing will save the deleted changes. Does anyone have any idea how to make it work?

1 个答案:

答案 0 :(得分:1)

You can delete it using remove. You will need to get the key/id field so without seeing the grid and assuming it is say in a hidden first column:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<main>
  <div>
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
    <img src="http://placehold.it/8x1" />
  </div>
</main>

Also note you can rewrite this code:

private void deleteRowsBtn_Click(object sender, EventArgs e)
{
    string delId;
    BSRecords deleteRecord;
    foreach (DataGridViewRow item in this.bSRecordsDataGridView.SelectedRows)
    {
        bSRecordsDataGridView.Rows.RemoveAt(item.Index);

        // code to remove record from database
        delId = item.Cells[0].Value.ToString();  // column that has id field
        deleteRecord = db.BSRecords.First(b => b.Id == delId);    // get the record. will throw exception if not found.
        db.BSRecords.Remove(deleteRecord);

    }
    db.SaveChanges();
    bSRecordsDataGridView.DataBind();   // this will refresh your grid. Do same in save.
}

with an object initializer:

BSRecords breakfastRecord = new BSRecords();
breakfastRecord.BS = brkBS.ToString();
breakfastRecord.Carbs = brkCarb.ToString();
breakfastRecord.Notes = brkftNoteTxt.Text;
breakfastRecord.Date = dateTxt.Text;