简体   繁体   English

删除DataGridView和数据库中的行

[英]Delete row in DataGridView and database

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. 我对数据库和EF完全陌生,但是我使用EF创建了数据库,并且在Windows窗体上具有DataGridView控件,该控件是通过将数据源拖动到窗体上而制成的。 After the user enters their information and hits the save button it succesfully saves their information in the database using 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();
   }
}

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 当用户在DataGridView中选择一行并单击具有此代码的删除按钮时

private void deleteRowsBtn_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow item in this.bSRecordsDataGridView.SelectedRows)
    {
        bSRecordsDataGridView.Rows.RemoveAt(item.Index);
    }
    db.SaveChanges();
}

It deletes the data in the DataGridView but doesn't save the changes in my database. 它会删除DataGridView中的数据,但不会将更改保存在我的数据库中。 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? 有谁知道如何使它工作?

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: 您将需要获取key / id字段,以便不查看网格并假设它在隐藏的第一列中说:

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

Also note you can rewrite this code: 另请注意,您可以重写以下代码:

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

with an object initializer: 使用对象初始化程序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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