我有这段代码
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它正在创建数据表及其行。不确定它是否正在做一些让我无法做我想做的事情。
答案 0 :(得分:6)
而不是ItemArray,只需访问索引:
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());
}
}
答案 1 :(得分:1)
这是foreach循环的缺点,您无法修改集合的内容,因为它仅限于循环内部。您可能需要使用常规for循环。根据此microsoft document“ foreach语句用于迭代集合以获取所需信息,但不应用于更改集合的内容以避免不可预测的副作用” 。
答案 2 :(得分:0)
如果使用
,会得到相同的结果吗?Debug.WriteLine(“val:”+ val +“,new:”+ row.ItemArray [i] .ToString());
试试这个
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());
}
}
答案 3 :(得分:0)
尝试在进行任何更改之前将readonly设置为false
dt.Rows [i] .ReadOnly = false
或仅限于细胞
dt.Rows [i] .ItemArray [j] .ReadOnly = false(不确定)
答案 4 :(得分:0)
这不会起作用,因为您正在修改数据图像
ds.Tables [0] .Rows [0] .ItemArray [1] =“新数据”
这将有效
ds.Tables [0] .Rows [0] .Item [1] =“新数据”;
确保您不使用itemarray,而是使用item。这适用于vb.net