DataGridView update datasource directly after changed Checkbox value

Jürgen Steinblock picture Jürgen Steinblock · Jan 3, 2011 · Viewed 13.2k times · Source

I have a System.Windows.Forms DataGridView that is bound to a List<MyObject>.
The class MyObject contains a boolean property that is bound to DataGridViewCheckboxCell within the DataGridView.

public class MyObject
{
    public decimal DefaultValue {get; set; }
    public bool HasCustomValue {get;set; }
    public decimal CustomValue {get;set; }
    public decimal CurrentValue
    {
        get
        {
            return HasCustomValue
                ? CustomValue
                : DefaultValue;
        }
}

If I change the value of HasCustomValue another (readonly) property CurrentValue changes it's value, too. That is done by implementing the INotifyPropertyChanged event (I left that part in the source example for simplicity)

If I changed HasCustomValue from outside the DataGridView, the column bound to CurrentValue gets updated immediately. Howevery, If the users enables/disables the checkbox, HasCustomValue is not changed in the underlying datasource unless he leaves the column by clicking with the mouse or pressing the TAB key.

Is there a way to force the grid to update the datasource directly after changing a checkbox value?

If I bind a Control Property I have the ability to set the DataSourceUpdateMode to Windows.Forms.DataSourceUpdateMode.OnPropertyChanged but I haven't found anything like that in a DataGridView

Answer

indiPy picture indiPy · Jan 3, 2011

I assume you are using bindingsource then on Check Box Click event/Edited do the ,

    BindingSource.EndEdit()