简体   繁体   中英

How to suspend updating a currently editing cell in the bounded DataGridView?

I have a DataGridView bound to the collection, which is changed frequently (not collection itself, but properties of the objects). When I start editing a cell, it quickly replaces with new value, which came from PropertyChanged event. Is there anyway to "suspend" currently editing cell from updating.

You should be able to achieve what you want by adding a BindingSource .

bindingSource1.DataSource = yourdatasource;
dataGridView1.DataSource = bindingSource1; 

Within the CellBeginEdit and CellEndEdit events of the datagridview you can then change the binding source's RaiseListChangedEvents property:

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    bindingSource1.RaiseListChangedEvents = true;
}

void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    bindingSource1.RaiseListChangedEvents = false;
}

I tested this with a background worker with a sleep inside it started by a button, and an update to the bound list after that. I pressed the button then edited a cell, and after the timer expired, my changes to the cell still held.

When I didn't begin editing the cell changes to the list were changed.


One thing to note is that this is for the whole binding source, not a particular cell.

If you always want the bound property to be updated only when you get out of the field (or when the field is updated from the code), then I think you should use DataBindingMode.OnValidated instead of DataBindingMode.OnPropertyChanged at the moment you create your binding.

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