0

I create a datatable like this:

strSQL = "My SQL String";                   
cmd.CommandText = strSQL;
DataTable dtBMP = new DataTable();
dtBMP.Load(cmd.ExecuteReader());

I'm looping through it, and I'd like to add data like this:

DataRow dr = dt.Rows[RowIndex];
dr[ColIndex] = "My Value";

But it keeps telling me that that cell is read-only. I've tried adding:

 dr.ReadOnly = false;

But then I get the error "'System.Data.DataRow' does not contain a definition for 'ReadOnly' and... etc."

Is there some straightforward way to create a datatable in read/write mode? Or some other way to solve this problem? Thanks.

buckshot
  • 315
  • 4
  • 15
  • You may have a connection string issue. Best way of read the query is using an adapter. See https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbdataadapter.fill?view=dotnet-plat-ext-3.1 – jdweng Jun 30 '20 at 16:59
  • 1
    ReadOnly is a DataColumn's property. Are you trying to change the PrimaryKey of that table? Did you try to change other columns? – Steve Jun 30 '20 at 17:02
  • It seems to do it with any column I try. – buckshot Jun 30 '20 at 17:02
  • See https://stackoverflow.com/questions/9220185/why-does-the-returned-datatable-has-readonly-columns-in-filehelpers/9249814 –  Jun 30 '20 at 17:44

1 Answers1

2

For anyone who comes here later, the solution is to change the readonly attribute of each column to false. Readonly is a column property. So:

foreach (DataColumn col in myTable.Columns) { col.ReadOnly = false; }
buckshot
  • 315
  • 4
  • 15