c# - 为什么 DataRow 在方法的一部分中被识别而不是在另一部分中(我如何动态添加 DataRows)?

标签 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 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;

...我使用第 1 行(第 2a 和 2b 行被注释掉)或使用第 2a 和 2b 行(第 1 行被注释掉)得到这个编译时错误:

'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' 由于其保护级别而无法访问

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

最佳答案

DataRow 的构造函数被标记为protected internal,因此,您不能直接构造它。

DataTable 获取新行的正确代码是

DataRow dr = dt.NewRow();

关于c# - 为什么 DataRow 在方法的一部分中被识别而不是在另一部分中(我如何动态添加 DataRows)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081875/

相关文章:

c# - 我想知道有没有更好的方法来实现这个 "simple lock"

c# - 在 C# 中扩展窗体

c# - 我如何使用 2 个 sql 命令和 1 个阅读器?

java - 如果一条记录插入失败,如何让Preparedstatement.executeBatch() 为好记录工作

sql - Oracle 10 如何更新一个表作为总和

oracle - sql 加载器日志文件结果

c# - 如何使函数返回 IEnumerable<string> 而不是 C# 中的字符串

c# - SHA512 计算哈希返回乱码

c# - C# 中的机器学习库

c# - 在运行时将 PictureBox 添加到窗体