简体   繁体   中英

DataGridView: c# generic UserDeletingRow for multiple grids on same form

I have several dgvs on a single form. 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"; 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.

I want to just have one generic sub to handle all of the dgvs. (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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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