Why is the DataRow recognized in one part of the method, but not in the other (how can I dynamically add DataRows)?

With this code:

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;

... I get this compile-time error using either line 1 (lines 2a and 2b are commented out) or using lines 2a and 2b (with line 1 missing):

'System.Data.DataRow.DataRow (System.Data.DataRowBuilder)' is unavailable due to the level of protection

This puzzles me, because a DataRow in a for loop is allowed. How can I add these DataRows to my OracleDataTable?

+5
source share
2 answers

The constructor for is DataRowmarked as protected internal, and as such you cannot build it directly.

The correct code to get a new line for DataTable-

DataRow dr = dt.NewRow();
+19

OracleDataTable, , DataTable.

DataRow , protected. factory, DataTable.Rows.Add DataTable.NewRow. , 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;
}
+3

All Articles