C# 为什么在方法的一个部分可以识别数据行,而在另一个部分不能识别数据行(如何动态添加数据行)?

C# 为什么在方法的一个部分可以识别数据行,而在另一个部分不能识别数据行(如何动态添加数据行)?,c#,winforms,oracle,dynamic,dotconnect,C#,Winforms,Oracle,Dynamic,Dotconnect,使用此代码: OracleDataTable dt = PlatypusSchedData.GetAvailablePlatypi(); OracleDataTable outputDt = new OracleDataTable(); int iRows = 12; while (iRows > 0) { outputDt.Rows.Add(new DataRow()); // line 1 //var dr = new DataRow(); // line

使用此代码:

OracleDataTable dt = PlatypusSchedData.GetAvailablePlatypi(); 
OracleDataTable outputDt = new OracleDataTable();

int iRows = 12;
while (iRows > 0)
{
    outputDt.Rows.Add(new DataRow()); // line 1
    //var dr = new DataRow();     // line 2a
    //outputDt.Rows.Add(dr);      // line 2b
    iRows -= 1;
}

for (int i = 0; i < dt.Rows.Count; i += 1) {
    DataRow dr = dt.Rows[i];
    int outputColumn = 0;
    if (i % 12 == 0 && i > 0) {
        outputColumn += 1; //2?
    }
    outputDt.Rows[i % 12][outputColumn] = dr[0];
    outputDt.Rows[i % 12][outputColumn + 1] = dr[1];
}
dataGridView1.DataSource = outputDt;
OracleDataTable dt=platypuscheddata.GetAvailablePlatypi();
OracleDataTable outputDt=新的OracleDataTable();
int iRows=12;
而(iRows>0)
{
outputDt.Rows.Add(new DataRow());//第1行
//var dr=new DataRow();//第2a行
//outputDt.Rows.Add(dr);//第2b行
iRows-=1;
}
对于(int i=0;i0){
outputColumn+=1;//2?
}
outputDt.Rows[i%12][outputColumn]=dr[0];
outputDt.Rows[i%12][outputColumn+1]=dr[1];
}
dataGridView1.DataSource=outputDt;
…我使用第1行(注释掉第2a和2b行)或第2a和2b行(注释掉第1行)得到这个编译时错误:

“System.Data.DataRow.DataRow(System.Data.DataRowBuilder)”由于其保护级别而不可访问


这让我感到困惑,因为for循环中的DataRow是可以容忍的。如何将这些数据行添加到我的OracleDataTable中?

数据行的构造函数被标记为受保护的内部构造函数,因此,您不能直接构造它

数据表
获取新行的正确代码为

DataRow dr = dt.NewRow();

我不熟悉
OracleDataTable
,但我假设它继承自一个普通的
DataTable

无法直接创建
DataRow
,因为构造函数是空的。相反,您必须使用工厂方法之一,如或。这可以确保在新的
DataRow
上应用正确的模式,因为它是根据其
DataTable
确定的

因此,这应该是可行的:

while (iRows > 0)
{
    DataRow dr = outputDt.NewRow();
    // fill the fields of the row ...
    // add it to the DataTable:
    outputDt.Rows.Add(dr);
    iRows -= 1;
}