开发者

DataGridView update datasource directly after changed Checkbox value

开发者 https://www.devze.com 2023-02-02 09:58 出处:网络
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.

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 MyOb开发者_开发知识库ject
{
    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


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

    BindingSource.EndEdit()


I had a similar problem. And I wasn't using a BindingSource, just a BindingList. After lots of frustration and experimentation (and following different solutions that didn't quite work),

I simply did this:

  • override the DataGridView's CellMouseUp event
  • in the event, call the DataGridView's EndEdit() method.


I did this trick:

  • Set the column's with CheckBox ReadOnly property to true.
  • Then in CellContentClick event handler programmatically change the value to its opposite value.

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int chkBoxColIdx = 0; //Index of the column with checkbox.
        if (e.ColumnIndex == chkBoxColIdx)
        {
            dataGridView1.Rows[e.RowIndex].Cells[chkBoxColIdx].Value = !(bool)dataGridView1.Rows[e.RowIndex].Cells[chkBoxColIdx].Value;
        }
    


Use handler for datagridview.CurrentCellDirtyStateChanged

private void datagridview_CurrentCellDirtyStateChanged(Object sender, EventArgs e)
{
    //_checkboxColumnIndex - index of your checkboxcolumn
    DataGridView dgv = (DataGridView)sender;
    if (_checkboxColumnIndex == dgv.CurrentCell.ColumnIndex &&
        dgv.Columns[_checkboxColumnIndex].GetType() == typeof(DataGridViewCheckBoxColumn) &&
        dgv.IsCurrentCellDirty == true)
    {          
        //Remember that here dgv.CurrentCell.Value is previous/old value yet
        YourObject.HasCustomValue = !(bool)dgv.CurrentCell.Value
    }

    dgv.CommitEdit(DataGridViewDataErrorContexts.Commit) //this will fire .CellEndEdit event
}


I can imagine that you don't want your bindingSource know to what it is bound to. After all, isn't that why you created a bindingSource: to be able to let it be bound to bound to virtually anything.

So naturally, you don't want to know how the value of your current item is changed; you only want to know that it has been changed.

For this you use event BindingSource.CurrentItemChanged: whatever method is used to change the data, you get notified.

The view that is bound to the BindingSource has to tell the BindingSource that changing the value is finished; editing the property has ended.

In your case the view is a DataGridView. A DataGridView tells the BindingSource that the current cell has finished changing using DataGridView.EndEdit().

Normally while you are typing the cell, the editing is ended when the cell loses focus, or when you press esc. This gives you the opportunity to correct typing errors or cancel editing in case you don't want the changes.

However in case of a DataGridViewCheckBoxCell most people expect to finish editing as soon as the DataGridviewCheckBoxCell is clicked.

Therefore you need to handle event DataGridView.CurrentCellDirtyStateChanged

// Whenever a DataGridViewCheckBoxCell becomes dirty editing is finished:
private void OnCurrentCellDirtyChanged(object sender, EventArgs e)
{
    if (this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell;
    {
        this.dataGridView1.EndEdit();
    }
}

This will lead to event BindingSource.CurrentItemChanged

0

精彩评论

暂无评论...
验证码 换一张
取 消