实体框架:C#Winforms绑定资源删除datagridview,但标记isDeleted字段为true(不删除)(Entity Framework: C# Winforms bindingsour

编程入门 行业动态 更新时间:2024-10-15 14:11:38
实体框架:C#Winforms绑定资源删除datagridview,但标记isDeleted字段为true(不删除)(Entity Framework: C# Winforms bindingsource delete on datagridview, but tag isDeleted field as true only (not delete))

我目前正在使用Entity Framework 5开发C#Winforms应用程序。

我的问题是如何删除bindingsource到entity的bindingsource上的项目的删除(只将标记为isDeleted字段为true )。

这是我的表单截图:

DataGridView绑定到enrollmedsBindingSource 。 请参阅填充绑定源的表单加载代码:

private void EnrollMedicationFrm_Load(object sender, EventArgs e) { context.enrollmeds.Where(adm => adm.FK_Admission == _SelectedPKAdm && adm.isDeleted == false).ToList(); enrollmedsBindingSource.DataSource = context.enrollmeds.Local; }

正如您在上面的代码中看到的,我过滤了数据以仅显示isDeleted设置为false的数据。

以下是我的删除按钮的代码:

private void DeleteBtn_Click(object sender, EventArgs e) { if (enrollmedsDataGridView.CurrentRow == null) { MessageBox.Show("No item selected.", "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { enrollmedsBindingSource.Remove(enrollmedsBindingSource.Current); } }

如果我调用context.SaveChanges();此代码将从数据库中删除该项context.SaveChanges(); 在保存按钮上。

以下是保存按钮的代码:

private void SaveBtn_Click(object sender, EventArgs e) { this.enrollmedsBindingSource.EndEdit(); context.enrollmeds.Local .Where(a => a.C__PK_EnrollMeds == 0) .ToList().ForEach(i => { i.FK_DC_Patient = pxdetails.pxDC.PK_Datacenter; i.FK_DC_userAdd = mainfrm.PK_DC_UserLoggedIn; i.AddDateTime = currdatetime; i.FK_Admission = pxdetails.adm.PK_Admission; }); this.context.SaveChanges(); enrollmedsDataGridView.Refresh(); this.Dispose(); }

我想要做的是删除DataGridView上的条目(从用户的视图),但只在数据库中标记isDeleted字段为true (用于审计目的)

注意: 只有在用户完成所有更改后才会单击保存按钮,因此表单需要捕获保存时删除( isDeleted ),更改和添加的内容。

I am currently working on a C# Winforms application with Entity Framework 5.

My problem is how to do a delete (which only tag isDeleted field as true) to an item on the bindingsource binded to an entity.

This is a screenshot of my form:

The DataGridView is bound to enrollmedsBindingSource. See this code of form load which fills the binding source:

private void EnrollMedicationFrm_Load(object sender, EventArgs e) { context.enrollmeds.Where(adm => adm.FK_Admission == _SelectedPKAdm && adm.isDeleted == false).ToList(); enrollmedsBindingSource.DataSource = context.enrollmeds.Local; }

As you can see on the above code, I filtered the data to display only the data which isDeleted is set to false.

Below is the code for my delete button:

private void DeleteBtn_Click(object sender, EventArgs e) { if (enrollmedsDataGridView.CurrentRow == null) { MessageBox.Show("No item selected.", "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { enrollmedsBindingSource.Remove(enrollmedsBindingSource.Current); } }

This code removes the item from the database if I call context.SaveChanges(); on save button.

Below is the code for save button:

private void SaveBtn_Click(object sender, EventArgs e) { this.enrollmedsBindingSource.EndEdit(); context.enrollmeds.Local .Where(a => a.C__PK_EnrollMeds == 0) .ToList().ForEach(i => { i.FK_DC_Patient = pxdetails.pxDC.PK_Datacenter; i.FK_DC_userAdd = mainfrm.PK_DC_UserLoggedIn; i.AddDateTime = currdatetime; i.FK_Admission = pxdetails.adm.PK_Admission; }); this.context.SaveChanges(); enrollmedsDataGridView.Refresh(); this.Dispose(); }

What I want to do, is to remove the entry on the DataGridView (from user's view) but only tag the isDeleted field is true in the database (for audit purposes).

Note: The save button only clicked once the user is done on all the changes, so the form need to capture what is deleted (isDeleted), changed, and added upon saving.

最满意答案

您应该将实体的IsDeleted属性设置为true并调用SaveChanges 。 例如,如果您有一个Product实体:

var p = (Product)bindingSource.Current; p.IsDeleted = true; db.SaveChanges();

然后,如果您不打算重新加载数据,可以从列表中删除该项以不显示它:

bs.RemoveCurrent(); db.Entry(p).State = EntityState.Detached;

或者您可以重新加载数据:

db.Products.Where(x => x.IsDeleted == false).Load(); bindingSource.DataSource = db.Products.Local;

注意

如果您不打算立即保存更改,则需要在将IsDeletet设置为true后执行其他操作:

您可以停止连接到上下文,并使用BindingList<T>或ObservableCollection<T>进行数据绑定并自行跟踪数据更改。 然后,您可以简单地获取已删除项目的列表,以便在保存更改时将其IsDeleted属性设置为true 。

另一种选择是,在将IsDeleetd设置为true后,您可以在DataGridView更改已删除行的外观。 例如,您可以处理DataGridView RowPostPaint事件并在该行上绘制一个红色三振出局。

You should set IsDeleted property of your entity to true and the call SaveChanges. For example, if you have a Product entity:

var p = (Product)bindingSource.Current; p.IsDeleted = true; db.SaveChanges();

Then if you are not going to reload data, you can remove the item from list to not show it:

bs.RemoveCurrent(); db.Entry(p).State = EntityState.Detached;

Or you can reload data:

db.Products.Where(x => x.IsDeleted == false).Load(); bindingSource.DataSource = db.Products.Local;

Note

If you are not going to save changes immediately, you need to do something else after setting IsDeletet to true:

You can stop working connected to context and use a BindingList<T> or ObservableCollection<T> for data-binding and track data changes yourself. Then you can simply have a list of deleted items to set their IsDeleted property to true when saving changes.

As another option, after setting IsDeleetd to true, you can change the appearance of deleted rows in DataGridView. For example, you can handle RowPostPaint event of DataGridView and draw a red strikeout like over the row.

更多推荐

本文发布于:2023-08-07 17:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   绑定   实体   标记   框架

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!