使用自定义DataRow提供错误'尝试访问元素作为与数组不兼容的类型。'

时间:2013-04-11 13:01:45

标签: c# datatable datarow

我正在尝试实现自己的DataRow课程。 DataRowDataTable类看起来像这样:

public class GenericFormFieldDataTable : TypedTableBase<GenericFormFieldDataRow>
{
    public IEnumerable<ExtraInfo> Information { get; set; }

    public GenericFormFieldDataTable(IEnumerable<ExtraInfo> info)
    {
        Information = info;
    }

    public void AddGenericFormFieldRow(GenericFormFieldDataRow row)
    {
        Rows.Add(row);
    }
    public GenericFormFieldDataRow AddGenericFormFieldRow(LineData data)
    {
        //THE LINE BELOW CAUSES THE CRASH
        GenericFormFieldDataRow newRow = ((GenericFormFieldDataRow)(NewRow()));
        List<object> values = new List<object>();
        ...

        Here we do things with values, Information and eventually fill datarow.LineData

        ...
        newRow.ItemArray = values.ToArray();
        Rows.Add(newRow);
        return newRow;
    }


    protected new GenericFormFieldDataRow NewRowFromBuilder(DataRowBuilder builder)
    {
        return new GenericFormFieldDataRow(builder);
    }

    protected override Type GetRowType()
    {
        return typeof (GenericFormFieldDataRow);
    }

    public GenericFormFieldDataRow this[int index]
    {
        get
        {
            return ((GenericFormFieldDataRow)(this.Rows[index]));
        }
    }
}

public class GenericFormFieldDataRow : DataRow
{
    public LineData LineData { get; set; }

    protected internal GenericFormFieldDataRow(DataRowBuilder builder) : base(builder) {}
}

问题是,当我们致电AddGenericFormFieldRow时,我们收到错误:

Attempted to access an element as a type incompatible with the array.

Call stack:
System.Data.DataTable.NewRow(Int32 record) +60
   System.Data.DataTable.NewRow() +16
   GenericFormFieldDataTable.AddGenericFormFieldRow(LineData data) in GenericFormFieldDataTable.cs:37

代码基于数据集设计器生成的内容。我在这里错过了什么/做错了什么?

0 个答案:

没有答案