【问题标题】:Fill DataTable using array of NewRows使用 NewRows 数组填充 DataTable
【发布时间】:2013-08-19 18:38:43
【问题描述】:

感谢您的帮助!我正在尝试在从 Microsoft Reporting Services 中提取报告之前填充 DataTable。

我原本以为我可以运行 for 查找(取决于我的“MainTable”中有多少行,并分配每个类别并将其刷新到 Rows.Add 并重复但显然我不能 Row.Add 相同的名称NewRow。这是我到目前为止所拥有的。感谢您的帮助!:

MyDataSet.ESSRow newESS = MyDataSet.ESS.NewESSRow();
        for (int i = 0; i < ds.Tables["MainTable"].Rows.Count; i++)
        {
            DataRow dRow = ds.Tables["MainTable"].Rows[i];
            if(Convert.ToInt32(dRow.ItemArray.GetValue(9).ToString()) > ShiftDelta)//checks if instance is longer than a shift 
            {
            newESS.Station = "7";
            newESS.Switch ="7";
            newESS.Start = dRow.ItemArray.GetValue(6).ToString();
            newESS.Stop = dRow.ItemArray.GetValue(7).ToString();
            newESS.SwitchIs = dRow.ItemArray.GetValue(8).ToString();
            TimeSpan t = TimeSpan.FromSeconds(Convert.ToInt32(dRow.ItemArray.GetValue(9).ToString()));
            newESS.Duration = string.Format("{0:D2}h:{1:D2}m:{2:D2}s", t.Hours, t.Minutes, t.Seconds);
            MyDataSet.ESS.Rows.Add(newESS);                 }

        }

【问题讨论】:

    标签: c# arrays reporting-services datatable


    【解决方案1】:

    还可以尝试使用 importRow,因为这不会给出恼人的行已经属于另一个表的错误。

    【讨论】:

      【解决方案2】:

      不要尝试多次添加同一行,而是创建一个新行以在循环的每次迭代中添加。这可以通过将newESS 的声明移动到循环的inside 而不是循环的outside 来完成。除此之外,您还应该将其移动到 if 语句的内部,这样如果您不打算添加新行,就不会创建新行。

      【讨论】:

      • 让我试一试,我会尽快回复!
      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 2020-03-05
      • 2014-10-12
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多