如下面的代码,TestDataRow row = (TestDataRow)this.NewRow(); 父类型(DataRow)被强制转化为子类型(TestDataRow),从语法上说不同,但是MSDN上就是这么写的。这段代码在我机器上通不过,有没有大虾能帮我看看
-代码哪里有问题
-为什么能这样转化class TestDataTable : System.Data.TypedTableBase<TestDataRow>
{
private DataColumn Name;
private DataColumn ID; public TestDataTable()
{
this.TableName = "TestDataSet";
this.BeginInit();
this.InitClass();
this.EndInit();
} private void InitClass()
{
this.Name = new global::System.Data.DataColumn("Name", typeof(string), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.Name);
this.ID = new global::System.Data.DataColumn("ID", typeof(string), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.ID); this.Constraints.Add(new global::System.Data.UniqueConstraint("Constraint1", new global::System.Data.DataColumn[] {
this.Name,
this.ID}, true));
this.Name.AllowDBNull = false;
this.ID.AllowDBNull = false;
} public DataColumn NameCoulmn
{
get
{
return this.Name;
}
} public DataColumn IDCoulmn
{
get
{
return this.ID;
}
} public TestDataRow this[int index]
{
get
{
return (TestDataRow)(this.Rows[index]);
}
} public TestDataRow addRow()
{
TestDataRow row = (TestDataRow)this.NewRow();
this.Rows.Add(row);
return row;
}
}
#endregion #region Row
class TestDataRow : DataRow
{
private TestDataTable testDataTable; internal TestDataRow(DataRowBuilder rb)
: base(rb)
{

this.testDataTable = ((TestDataTable)(this.Table));
} public string Name
{
get
{
return (string)this[this.testDataTable.NameCoulmn];
}
set
{
this[this.testDataTable.NameCoulmn] = value;
}
} public string ID
{
get
{
return (string)this[this.testDataTable.IDCoulmn];
}
set
{
this[this.testDataTable.IDCoulmn] = value;
}
}
}
#endregion

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-cn/library/system.data.datatable.newrow(v=vs.80).aspx
      

  2.   


    Thanks您给的link上面很容易理解
     row = table.NewRow();
            row["id"] = i;
            row["item"] = "item " + i.ToString();
            table.Rows.Add(row);
    这里的NewRow()也是一个datarow object,table.rows也是datarowcollection,完全没有问题
    我的问题是:
    我需要的是自己定义的row,把它们加入rows里面,取出来的时候可以直接就是mydatarow,照您link上的话,取出来还是datarow,还是有一个专程子类对象的过程。
      

  3.   

    用List<TestDataRow>试试 
      

  4.   


    this.Rows.Add(row);
    是System.Data.TypedTableBase中定义的,不可以更改,并且为了通用性,我也觉得哦那个datarow是最好的。
    我就是想问(TestDataRow)this.NewRow();是怎么实现转化的