简体   繁体   English

DataGridView:c#通用UserDeletingRow用于同一表单上的多个网格

[英]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.

相关问题 DataGridView UserDeletingRow不触发 - DataGridView UserDeletingRow Not Firing c# windows 表单应用中将通用列表项绑定到不同网格 - Binding items of generic list to different grids in c# windows form application 如何在C#中同时将datagridview的多个单元格值从一种形式传递到另一种形式? - How can I pass multiple cell values of datagridview from one form to another at same time in C#? C# WPF - 为在不同网格中定义的多个复选框创建通用样式触发器 - C# WPF - Create a generic style trigger for multiple checkboxes defined within separate grids UserDeletingRow DataGridView导致程序终止 - UserDeletingRow DataGridView causing program to terminate 使用C#在Gridview中添加多个网格 - Add multiple Grids in Gridview using C# c#以相同的形式插入,删除和编辑加载datagridview - c# Insert,Delete, and Edit in Same form load datagridview C#:是否可以对同一个泛型变量有多个泛型约束? - C#: Is it possible to have multiple generic constraints on the same generic variable? 在datagridview C#表格上的一行中保持多个数据 - Holding still multiple datas in one row on datagridview C# Form 在c#窗口形式的datagridview中一个接一个地添加多个项目 - adding multiple items one by one in datagridview in c# window form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM