[英]DataGridView: c# generic UserDeletingRow for multiple grids on same form
I have several dgvs on a single form. 我在一个表格上有几个dgv。 They all have bound data source.
它们都有绑定的数据源。 They all need to have the ability to "soft" delete rows (not really delete them, but mark the row for deletion and hide it on the grid).
他们都需要具有“软”删除行的能力(不是真正删除行,而是将行标记为要删除并将其隐藏在网格中)。 The delete will happen at a later date.
删除将在以后发生。
I do have a filter on the on the grid: hiatusBindingSource.Filter = "IsDeleted = false";
我在网格上确实有一个过滤器:
hiatusBindingSource.Filter = "IsDeleted = false";
But I am not sure how to apply the filter after I set the "delete" flag so I came up with the following code to handle a specific dgv. 但是我不确定设置“ delete”标志后如何应用过滤器,因此我想出了以下代码来处理特定的dgv。
I want to just have one generic sub to handle all of the dgvs. 我只想拥有一个通用子即可处理所有dgv。 (I have multiple forms with the same issue.)
(我有多个表格有相同的问题。)
private void dgvHiatus_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
e.Cancel = true;
hiatusBindingSource.SuspendBinding();
e.Row.Visible = false;
hiatusBindingSource.ResumeBinding();
((Hiatus)e.Row.DataBoundItem).IsDeleted = true;
SetFormMode(Globals.FormStatusMode.Save);
}
TIA TIA
If all of your DataGridView are bounded using a BindingSouce and the classes used as DataSource for the BindingSource derive a common base class that has defined the IsDeleted property then I suppose you could make just one event handler for all your DataGrid with code like this 如果使用BindingSouce绑定所有DataGridView,并且用作BindingSource的数据源的类派生了定义了IsDeleted属性的公共基类,那么我想您可以使用以下代码为所有DataGrid制作一个事件处理程序
private void common_dgv_DeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
e.Cancel = true;
DataGridView dgv = sender as DataGridView;
BindingSource bs = dgv.DataSource as BindingSource;
bs.SuspendBinding();
e.Row.Visible = false;
bs.ResumeBinding();
((basex)e.Row.DataBoundItem).IsDeleted = true;
SetFormMode(Globals.FormStatusMode.Save);
}
Of course I am not able to test it but you could comment here if you have any difficulty. 当然我无法测试它,但是如果您有任何困难,可以在这里评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.