Add Columns to new DataRow

KodeKreachor picture KodeKreachor · Apr 11, 2012 · Viewed 19k times · Source

Is it possible to create a new DataRow object and add columns to it at runtime?

// How can I specify column names for this data row object?
DataRow row = new DataRow();

Answer

KeithS picture KeithS · Apr 11, 2012

No. A DataRow is designed to be a child of a DataTable, which has the accessors needed to add columns. If you could manipulate DataRows directly, it would be possible to create a "jagged table" with rows having different column counts/orders. This is generally a bad thing, so it's just not done.

If you want to add a column to your DataRow, add your Row to a DataTable, add a column to that DataTable, then look at your DataRow again.