September 20, 2016

C# DataTable ReadOnlyException - "Column X is read only."

While using typed DataSets you might encounter this exception.

    Column X is read only.

Its because the default value for ReadOnly property of DataColumn in Typed DataSets is True. You can set it False in the properties window, but this will take impact in all code segments wherever this DataTable is being used.

So selectively setting ReadOnly property to false is a better option if you need this in only some parts of the program. You can loop through all columns of the table and set its ReadOnly property to false:

 foreach (DataColumn column in table.Columns) 
 {
    column.ReadOnly = false; 
 }

Or you can set for single column at a time:

    table.Columns["MyColumnName"].ReadOnly = false;