以编程方式创建DataTable列MaxLength错误

时间:2013-03-11 17:14:52

标签: c# asp.net

我遇到需要修改现有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;
}

1 个答案:

答案 0 :(得分:1)

你的专栏:

drOutput[column.MaxLength]

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

尝试:

drOutput.Table.Columns[column.ColumnName].MaxLength = some_length;