DataGridViewColumn EditingControl 编辑值不反映

DataGridViewColumn EditingControl edited value not reflecting

我有一个用户控件,我需要将其添加为 DataGridView 中的一列。我按照 here 所示的说明进行操作。

控件已正确添加,当我单击该单元格时,会显示用户控件。但是,现在当我单击别处时,编辑后的值不会反映回 DataGridView。代码如下。 MoneyTextBox 是用户控件。

class MoneyTextBoxColumn : DataGridViewColumn
{
    public MoneyTextBoxColumn() : base(new MoneyTextBoxCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a MoneyTextBoxCell.
            if (value != null && !value.GetType().IsAssignableFrom(typeof(MoneyTextBoxCell)))
            {
                throw new InvalidCastException("Must be a MoneyTextBoxCell");
            }
            base.CellTemplate = value;
        }
    }
}

public class MoneyTextBoxCell : DataGridViewTextBoxCell
{

    public MoneyTextBoxCell() : base()
    {
    }

    public override void InitializeEditingControl(int rowIndex, object
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        MoneyTextBoxEditingControl ctl = DataGridView.EditingControl as MoneyTextBoxEditingControl;
        if (this.RowIndex >= 0)
        {
            if (!object.ReferenceEquals(this.Value, DBNull.Value) && this.Value != null && this.Value.ToString() != string.Empty)
            {
                ctl.Text = this.Value.ToString();
            }
            else
            {
                ctl.Text = this.DefaultNewRowValue.ToString();
            }
        }
    }


    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that MoneyTextBoxCell uses.
            return typeof(MoneyTextBoxEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that MoneyTextBoxCell contains.

            return typeof(string);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            return "0.00";
        }
    }
}

class MoneyTextBoxEditingControl : MoneyTextBox, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public MoneyTextBoxEditingControl()
    {
    }

    // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
    // property.
    public object EditingControlFormattedValue
    {
        get
        {
            return this.Value;
        }
        set
        {
            this.MText = (string)value;
        }
    }

    // Implements the 
    // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
    public object GetEditingControlFormattedValue(
        DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    // Implements the 
    // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
    }

    // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
    // property.
    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
    // method.
    public bool EditingControlWantsInputKey(
        Keys key, bool dataGridViewWantsInputKey)
    {
        // Let the DateTimePicker handle the keys listed.
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
    // method.
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    // Implements the IDataGridViewEditingControl
    // .RepositionEditingControlOnValueChange property.
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlDataGridView property.
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlValueChanged property.
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingPanelCursor property.
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }      
}

如何将编辑后的值传播回 DataGridView?

我接近实现我想要的目标的唯一方法是在 DetachEditingControl 方法中手动分配值,如下所示,但是,我得到 System.InvalidOperationException 消息 Operation is not valid due to the current state of the object,选择最后一行时。

public override void DetachEditingControl()
{
    DataGridView dataGridView = this.DataGridView;
    if (dataGridView == null || dataGridView.EditingControl == null)
    {
        throw new InvalidOperationException("Cell is detached or its grid has no editing control.");
    }
    MoneyTextBoxEditingControl ctl = DataGridView.EditingControl as MoneyTextBoxEditingControl;
    dataGridView.CurrentCell.Value = ctl.EditingControlFormattedValue;


    base.DetachEditingControl();
}

我的要求只是将编辑后的值返回给DataGridView。 TIA.

如果 DataGridView 是数据绑定的,您将无法修改单元格的内容。相反,您应该修改数据绑定对象。您可以通过 DataGridViewRow 的 DataBoundItem 访问该对象:

MyObj obj = (MyObj)dataGridView.CurrentRow.DataBoundItem;
obj.MyProperty = newValue;

绑定对象应实现 INotifyPropertyChanged 以便更改反映在 DataGridView

您可能会在此 post 中找到一些其他解决方案: How to programmatically set cell value in DataGridView?