Risho
Risho

Reputation: 2667

Programmatically Created DataTable Column MaxLength Error

I've got a situation where I need to modify an existing DataTable. @Shai Cohen has provided the code but because live data differs I get an exception MaxLimit violation on the column I'm modifying.

I've tried to change the with in the code but it keeps on throwing the error. Here is the code (See my comments within):

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;
}

Upvotes: 0

Views: 2230

Answers (1)

Grant Winney
Grant Winney

Reputation: 66511

Your line:

drOutput[column.MaxLength]

Is trying to grab a particular column from the row... in this case, not the first column or second column, but the column.MaxLength column.

Try:

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

Upvotes: 1

Related Questions