我創建了一個自定義控件來編輯DataGridView單元格中的自定義值。我在這裏的例子如下: How to: Host Controls in Windows Forms DataGridView CellsDataGridView單元格中的自定義控件在編輯時拋出FormatException
我有一個自定義的數據類是綁定到DataGridView的對象列表的成員。
internal class CustomValue
{
// Some stuff is here.
}
我創建了一個自定義控件來編輯實現IDataGridViewEditingControl接口的單元格中的值。
internal partial class CustomValueEditControl : UserControl, IDataGridViewEditingControl
{
DataGridView m_dataGridView;
private bool m_valueChanged = false;
int m_rowIndex;
private CustomValue m_value;
public CustomValue Value
{
get
{
return m_value;
}
set
{
m_value = value;
}
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
}
public DataGridView EditingControlDataGridView
{
get
{
return m_dataGridView;
}
set
{
m_dataGridView = value;
}
}
public object EditingControlFormattedValue
{
get
{
return this.Value.ToString();
}
set
{
if (value is String)
{
CustomValue val;
if (CustomValue.TryParse((String)value, out val))
{
this.Value = val;
}
else
{
this.Value = new CustomValue();
}
}
}
}
public int EditingControlRowIndex
{
get
{
return m_rowIndex;
}
set
{
m_rowIndex = value;
}
}
public bool EditingControlValueChanged
{
get
{
return m_valueChanged;
}
set
{
m_valueChanged = value;
}
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Up:
case Keys.Down:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
}
我已經設置了一個自定義的DataGridViewCell從DataGridViewTextBoxCell下降,從而所述細胞只顯示自定義值的字符串表示,直到它被編輯。
internal class CustomValueCell : DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CustomValueEditControl customValueEditControl = DataGridView.EditingControl as CustomValueEditControl;
if (this.Value == null)
{
customValueEditControl.Value = (CustomValue)this.DefaultNewRowValue;
}
else
{
customValueEditControl.Value = (CustomValue)this.Value;
}
}
public override Type EditType
{
get
{
return typeof(CustomValueEditControl);
}
}
public override Type ValueType
{
get
{
return typeof(CustomValue);
}
}
public override Type FormattedValueType
{
get
{
return typeof(string);
}
}
public override object DefaultNewRowValue
{
get
{
return new CustomValue();
}
}
}
我已經創建了一個自定義的DataGridViewColumn。
internal class CustomValueColumn : DataGridViewColumn
{
public CustomValueColumn()
: base(new CustomValueCell())
{
}
public override object Clone()
{
CustomValueColumn clone = (CustomValueColumn)base.Clone();
return clone;
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CustomValueCell.
if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomValueCell)))
{
throw new InvalidCastException("Must be a CustomValueCell");
}
base.CellTemplate = value;
}
}
}
單元格正確顯示自定義類型的值,並且我可以使用自定義控件編輯該值。當我離開編輯單元格時,出現System.FormatException:System.Convert.DefaultToType(IConvertable value,Type targetType,IFormatProvider provider)從'System.String'無效強制轉換爲'CustomValue'...
At它試圖將字符串轉換爲我的自定義值有什麼意義?這不應該由CustomValueEditControl.GetEditingControlFormattedValue來處理嗎?
這已經有一段時間,因爲我不得不這樣的事情,但它聽起來像是你需要重寫'DataGridViewCell'的'GetFormattedValue'方法,看一下吧[這裏]的部分(https://開頭msdn.microsoft.com/en-us/library/aa730881(v=vs.80).aspx#dtH1)(接近中間) –
我試過了。我認爲DataGridViewTextBoxCell已經覆蓋了從對象返回一個字符串。 –