Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a DataRow recognized in one part of a method but not another (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 commented out) or using lines 2a and 2b (with line 1 commented out):

'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

This is baffling me because the DataRow in the for loop is tolerated. How can I add these DataRows to my OracleDataTable?

like image 268
B. Clay Shannon-B. Crow Raven Avatar asked Aug 22 '12 21:08

B. Clay Shannon-B. Crow Raven


People also ask

What is a DataRow?

A data row is an individual data source record. Rows contain data and command cells. Data cells display field values.

How do you make a DataRow?

To create a new DataRow, use the NewRow method of the DataTable object. After creating a new DataRow, use the Add method to add the new DataRow to the DataRowCollection. Finally, call the AcceptChanges method of the DataTable object to confirm the addition.

How do you initialize a row of data?

A DataRow is created by calling the NewRow( ) method of a DataTable , a method that takes no arguments. The DataTable supplies the schema, and the new DataRow is created with default or empty values for the fields: // create a table with one column DataTable dt = new DataTable(); dt. Columns.


1 Answers

The constructor for DataRow is marked as protected internal, and as such, you cannot construct it directly.

The correct code to get a new row for a DataTable is

DataRow dr = dt.NewRow();
like image 178
Steve Czetty Avatar answered Oct 13 '22 00:10

Steve Czetty