我有這樣的代碼爲什麼在我的數據錶行中沒有更新該值?
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());
}
}
但從來沒有得到更新的價值。我不知道爲什麼。
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
編輯
一些更多的嘗試
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
闖闖
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());
}
}
給我
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:
所以也許這些東西是隻讀或東西?
我正在使用filehelpers它正在創建數據表,它是行。不知道它是否在做一些讓我無法做我想做的事的時髦。
感謝您發佈此信息。我從來沒有見過ItemArray。打算髮布,好吧,這個。 :) – 2012-02-09 22:30:44
看我的編輯。給我一個只讀錯誤。 – chobo2 2012-02-09 22:32:21
@ chobo2這對我的測試有效。鑑於你的錯誤,你的「列」被設置爲「ReadOnly = True」。所以你需要關閉它,或者顯然不要嘗試改變只讀列。 – LarsTech 2012-02-09 22:36:27