From the archives · C# and DataTable

C# : Adding a new column in datatable and adding value to it

Add a column to a populated DataTable and fill it with a value for every existing row.

When a query has already populated a DataTable, you can add another column to its schema and then assign a value to that column for each row.

DataSet dataSet = GetResultsDS(sql);

if (dataSet != null && dataSet.Tables.Count > 0)
{
    DataTable table = dataSet.Tables[0];
    table.Columns.Add("DrillDown", typeof(string));

    foreach (DataRow row in table.Rows)
    {
        row["DrillDown"] = "Value for new column";
    }
}

Columns.Add creates the new DrillDown column with the string data type. The loop then sets its value on every existing row. Replace the example text with the value or calculation you need for each row.

Note: Check that the DataSet is not null and contains at least one table before accessing Tables[0]. Use the appropriate table index or name if your query returns multiple tables.