Why is the value not updated in my datatable string?

I have this code

foreach (DataRow row in dt.Rows) { Debug.WriteLine("Start of Row"); for (int i = 0; i < row.ItemArray.Length; i++) { string val = row.ItemArray[i].ToString(); row.ItemArray[i] = "My New Value"; Debug.WriteLine("val: {0}, new: {1}", val, row.ItemArray[i].ToString()); } } 

but the value is never updated. I'm not sure why.

 Start of Row val: 2/02/2012, new: 2/02/2012 val: ac, new: ac val: ac, new: ac val: 515.00, new: 515.00 

Edit

A few more attempts

  for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = dt.Rows[i]; for (int x = 0; x < row.ItemArray.Length; x++) { string val = row.ItemArray[x].ToString(); row.ItemArray.SetValue("My New Value", x); Debug.WriteLine("val: {0}, new: {1}", val, row.ItemArray[x].ToString()); } } val: 2/02/2012, new: 2/02/2012 val: ac, new: ac val: ac, new: ac val: 515.00, new: 515.00 

Another attempt

  for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = dt.Rows[i]; for (int x = 0; x < row.ItemArray.Length; x++) { string val = row.ItemArray[x].ToString(); row[i] = "My New Value"; Debug.WriteLine("val: {0}, new: {1}", val, row.ItemArray[x].ToString()); } } 

gives me

 System.Data.ReadOnlyException was unhandled by user code Message=Column 'Date' is read only. Source=System.Data StackTrace: at System.Data.DataRow.set_Item(DataColumn column, Object value) at System.Data.DataRow.set_Item(Int32 columnIndex, Object value) at CCRecomendator.Framework.Services.CreditCardRecommendatorService.ParseTransactions(Stream stream) in at CCRecomendator.WebUI.Controllers.RecommendController.UploadTransactions(HttpPostedFileBase file) in at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) InnerException: 

So maybe read-only material or something else?

I use filehelpers , it creates data and rows. Not sure if he is doing something scared that is stopping me from doing what I want to do.

+1
source share
5 answers

Instead of ItemArray, just enter the index:

 foreach (DataRow row in dt.Rows) { Debug.WriteLine("Start of Row"); for (int i = 0; i < row.ItemArray.Length; i++) { string val = row[i].ToString(); row[i] = "My New Value"; Debug.WriteLine("val: {0}, new: {1}", val, row[i].ToString()); } } 
+6
source

This is a flaw with the foreach loop; you cannot modify the contents of a collection because it is limited only within the loop. You may need to use a regular loop. According to this microsoft document , the foreach statement is used to iterate through the collection to obtain the required information, but should not be used to modify the contents of the collection to avoid unpredictable side effects .

+1
source

Do you get the same result if you use

Debug.WriteLine ("val:" + val + ", new:" + row.ItemArray [i] .ToString ());

try it

 for (i=0; i< dt.Rows.Count ; i++) { Debug.WriteLine("Start of Row"); for (int j = 0; j < dt.Rows[i].ItemArray.Length; j++) { string val = dt.Rows[i].ItemArray[j].ToString(); dt.Rows[i].ItemArray[j] = "My New Value"; Debug.WriteLine("val: {0}, new: {1}", val, dt.Rows[i].ItemArray[j].ToString()); } } 
0
source

try setting readonly to false before making any changes.

dt.Rows [i] .ReadOnly = false

or just a cell

dt.Rows [i] .ItemArray [j] .ReadOnly = false (not sure about this)

0
source

This will not work when changing the data image.

ds.Tables [0] .Rows [0] .ItemArray [1] = "New Data"

It will work

ds.Tables [0] .Rows [0] .Item [1] = "New Data";

Make sure you are not using itemarray, use an element instead. This is for vb.net

0
source

Source: https://habr.com/ru/post/908107/


All Articles