我有一个用户控件,我需要在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
消息{{1} ,当选择最后一行时。
Operation is not valid due to the current state of the object
我的要求只是将编辑后的值恢复为DataGridView。 TIA。
答案 0 :(得分:1)
如果DataGridView是数据绑定的,那么您将无法修改单元格的内容。相反,您应该修改数据绑定对象。您可以通过DataGridViewRow的DataBoundItem访问该对象:
MyObj obj = (MyObj)dataGridView.CurrentRow.DataBoundItem;
obj.MyProperty = newValue;
绑定对象应实现INotifyPropertyChanged
,以便更改反映在DataGridView
您可以在这篇文章中找到其他一些解决方案: How to programmatically set cell value in DataGridView?