如何使用LINQ to SQL在DataGridView中编辑数据库记录?

时间:2021-03-05 01:44:38

I'm so close on this one, but I need a little help. I have a simple WinForms app that uses LINQ to SQL to grab data that needs to be reviewed from a database and stuff it into a DataGridView. That part works great, but the connection I can't seem to make is how to get the data back into the database when things change.

我很接近这个,但我需要一些帮助。我有一个简单的WinForms应用程序,它使用LINQ to SQL来获取需要从数据库中查看的数据并将其填充到DataGridView中。这部分工作得很好,但我似乎无法做出的连接是当事情发生变化时如何将数据恢复到数据库中。

Here's what I have so far:

这是我到目前为止所拥有的:

db = new SiteDataDataContext();
src = new BindingSource();
src.DataSource = GetSitesPendingApproval(); // Returns IQueryable<Site>.
dataGridView1.DataSource = src;
dataGridView1.AllowUserToDeleteRows = true;
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;

I have two actions I want to be able to take:

我有两个我想要采取的行动:

  1. When the checkbox for the boolean value IsActive (DataGridView.Columns[7]) is checked/unchecked I want changes to be sent to the database.
  2. 当选中/取消选中布尔值IsActive(DataGridView.Columns [7])的复选框时,我希望将更改发送到数据库。

  3. When a record is deleted from the datagridview, I want it to also be deleted from the database.
  4. 从datagridview中删除记录时,我希望它也可以从数据库中删除。

I'm obviously missing some sort of binding, submit, merge, append, or cell changed event handler, but I haven't been able to figure out the connection to send back the changes. If it's easier to just make all the changes and then hit some sort of submit button to send it all back that's fine too.

我显然缺少某种绑定,提交,合并,追加或单元格更改事件处理程序,但我无法找出连接以发回更改。如果更容易进行所有更改,然后点击某种提交按钮将其全部发送回来也很好。

1 个解决方案

#1


Have you tried adding an event handler for the UserDeletingRow event that uses the id on the row to remove the row from the database?

您是否尝试为UserDeletingRow事件添加事件处理程序,该事件处理程序使用行上的id从数据库中删除行?

private void DataGrid_UserDeletingRow(object sender,
                                           DataGridCommandEventArgs e)
{
    TableCell itemCell = e.Item.Cells[0]; // assumes id is in column 0

    int id = int.Parse(item.Text); // assumes it's non-null

    using (var dc = new SiteDataDataContext())
    {
         var site = dc.Sites.Where(s => s.ID == id).SingleOrDefault();

         if (site != null)
         {
             try
             {
                dc.Sites.DeleteOnSubmit( site );
                dc.SubmitChanges();
                dataGridView1.Bind();
             }
             catch (SqlException)
             {
                e.Cancel = true;
             }
         }
     }
}

You might also provide an error message if the delete failed, but how you do it would depend on your application.

如果删除失败,您可能还会提供错误消息,但是如何执行此操作取决于您的应用程序。

#1


Have you tried adding an event handler for the UserDeletingRow event that uses the id on the row to remove the row from the database?

您是否尝试为UserDeletingRow事件添加事件处理程序,该事件处理程序使用行上的id从数据库中删除行?

private void DataGrid_UserDeletingRow(object sender,
                                           DataGridCommandEventArgs e)
{
    TableCell itemCell = e.Item.Cells[0]; // assumes id is in column 0

    int id = int.Parse(item.Text); // assumes it's non-null

    using (var dc = new SiteDataDataContext())
    {
         var site = dc.Sites.Where(s => s.ID == id).SingleOrDefault();

         if (site != null)
         {
             try
             {
                dc.Sites.DeleteOnSubmit( site );
                dc.SubmitChanges();
                dataGridView1.Bind();
             }
             catch (SqlException)
             {
                e.Cancel = true;
             }
         }
     }
}

You might also provide an error message if the delete failed, but how you do it would depend on your application.

如果删除失败,您可能还会提供错误消息,但是如何执行此操作取决于您的应用程序。