0

我有一种情况需要修改现有的DataTable. @Shai Cohen 提供了代码,但由于实时数据不同,我正在修改的列上出现异常 MaxLimit 违规。

我试图更改代码中的 with 但它不断抛出错误。这是代码(请参阅我的评论):

public DataTable PrepareDataTable(DataTable dtResults)
{
   string[] subCategories = new string[3] {"Critical Down Time", "Critical Outage", "Total Repair Time"};

    DataTable dtOutput = dtResults.Clone();
    DataRow drOutput = null;
    DataRow[] drResults = null;
    var categories = dtResults.AsEnumerable().Select(r => r["Category"]).Distinct().ToList();

    foreach (string category in categories)
    {
        for (int i = 0; i < subCategories.Length    ; i++)
        {
            drOutput = dtOutput.NewRow();
            drOutput["Category"] = category;
            drOutput["SubCategory"] = subCategories[i];
            drResults = dtResults.Select(String.Format("Category = '{0}' AND SubCategory = '{1}'", category, subCategories[i]));
            if(drResults.Length > 0)
            {
                foreach(DataColumn column in dtResults.Columns)
                {

                     drOutput[column.ColumnName] = drResults[0][column.ColumnName];
    **I've tried**   drOutput[column.MaxLength] = Unit.Pixel(500);
    **or just a number but no use**       

                }
            }
     **Error >>>** dtOutput.Rows.Add(drOutput);
        }

        drOutput = dtOutput.NewRow();
        dtOutput.Rows.Add(drOutput);
    }
    return dtOutput;
}
4

1 回答 1

1

您的线路:

drOutput[column.MaxLength]

正在尝试从行中获取特定列...在这种情况下,不是第一列或第二列,而是 column.MaxLength 列。

尝试:

drOutput.Table.Columns[column.ColumnName].MaxLength = some_length;
于 2013-03-11T17:23:01.867 回答