繁体   English   中英

DataGridView 试图从字符串转换为对象

[英]DataGridView trying to cast from string to object

我有一个自定义控件,其Value属性是一个对象。 该控件还有一个Text属性,它根据对象显示对象的字符串属性。

这个自定义控件托管在DataGridView ,我实现了所需的接口IDataGridViewEditingControl以使其工作。 我还有 2 个继承自DataGridViewColumnDataGridViewTextBoxCell

CustomerTypeDto 类:

public class CustomerTypeDto
{
    public int Id {get; set;}
    public int Description {get; set}
    //Other properties...
}

剩下的一个问题是,在我从控件中选择一个值并且 DataGridView 尝试结束单元格编辑后,我收到以下异常:

System.FormatException:从“System.String”到“CustomerTypeDto”的无效转换。 ---> System.InvalidCastException:从“System.String”到“CustomerTypeDto”的无效转换。 在 System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo) --- 内部异常堆栈跟踪结束 --- 在 System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo ) 在 System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue) at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType,在 System.Windows.Forms.DataGridViewC 中键入 sourceType、TypeConverter targetConverter、TypeConverter sourceConverter、IFormatProvider formatInfo、Object formattedNullValue、Object dataSourceNullValue) ell.ParseFormattedValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter) 在 System.Windows.Forms.DataGridViewCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeGridConverter. .PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception&异常)

我需要重写哪个方法或属性,以便DataGridView可以从我的对象转换为字符串,反之亦然。

我应该从DataGridViewTextBoxCell还是直接从DataGridViewCell继承?

编辑

这是我的单元格类:

public class CustomerTypeCell : DataGridViewTextBoxCell
{
    public CustomerTypeCell()
        : base()
    { }

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

        CustomControl ctl = DataGridView.EditingControl as CustomControl;

        if (this.Value == null)
            ctl.Value = (CustomerTypeDto)this.DefaultNewRowValue;
        else
            ctl.Value = (CustomerTypeDto)this.Value;
    }

    public override Type EditType
    {
        get { return typeof(CustomControl); }
    }

    public override Type ValueType
    {
        get { return typeof(CustomerTypeDto); }
    }

    public override object DefaultNewRowValue
    {
        get { return null; }
    }

    public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
    {
        return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
    }
}

DataGridViewTextBoxCell的属性FormattedValueType始终返回字符串。

在上面的ParseFormattedValue方法中,它试图从字符串转换为我的对象。 此方法的说明:

将格式化为显示的值转换为实际的单元格值。

但这没有意义,因为ValueCustomerTypeDto类型,那么这种解析将如何工作?

基本上我想要做的是让用户从我的自定义控件中选择一个CustomerType对象。 该对象应该是单元格的值,并且值的文本(在本例中为Description属性)在单元格中显示为字符串。

如果我已经在单元格的 value 属性中有对象,我不明白为什么DataGridView想要解析对象中的字符串。

您的CustomerTypeDto类需要一个用于字符串类型的显式转换运算符。

class CustomerTypeDto
{
    // string -> CustomerTypeDto
    public static explicit operator CustomerTypeDto(string s)
    {
        CustomerTypeDto ctd = new CustomerTypeDto();
        // ... do something with the string.
        return ctd;
    }
    // CustomerTypeDto -> string
    public static explicit operator String(CustomerTypeDto ctd)
    {
        return ctd.toString();
        // or some other way to return it's string value.
    }
    // other stuff...
}

所以你可以做这样的事情:

return (CustomerTypeDto)someString;

如果您想提前实现您的价值,请使用方法e.ParsingApplied = true; 在您实现e.Value = (ObjOfYourType)我在删除异常之前引起的事件CellParsing中使用了它。

这个问题已经在 DataGridView Cell Throws FormatException When Editing中的Custom Control 中的2 个工作解决方案中得到了回答

第一个答案https://stackoverflow.com/a/36656360仅适用于我的评论,因此完整的解决方案是

public object GetEditingControlFormattedValue (DataGridViewDataErrorContexts context)
{
  if ((context & DataGridViewDataErrorContexts.Parsing) != 0)
  {
    // Here you should not return string, but your value
    return Value;
  }
  return EditingControlFormattedValue;
}

public override object ParseFormattedValue (object formattedValue,
                                            DataGridViewCellStyle cellStyle,
                                            TypeConverter formattedValueTypeConverter,
                                            TypeConverter valueTypeConverter)
{
  if (formattedValue is CustomerTypeDto)
  {
    return formattedValue;
  }
  return base.ParseFormattedValue (formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}

第二个答案https://stackoverflow.com/a/36668621按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM