在C#中为数据行分配新值 [英] To assign datarow with new value in C#
本文介绍了在C#中为数据行分配新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找数据表并逐行获取,
我有50个长度值。我想将第10个长度值指定为text。
但是在分配后它没有存储在datarow中
iam looking into datatable and fetching line by line,
iam having 50 length values. i want to assign 10th length value as "text".
but it is not stored in datarow after assigned
foreach (DataRow row in dtSource.Rows)
{
if (row.ItemArray[17].ToString().Length > 32)
{
string ss= "text";
row.ItemArray[17] = ss; // here it is not added in itemarray
}
}
}
我尝试过:
What I have tried:
foreach (DataRow row in dtSource.Rows)
{
if (row.ItemArray[17].ToString().Length > 32)
{
string ss= "text";
row.ItemArray[17] = ss; // here it is not added in itemarray
}
}
}
推荐答案
引用 row.ItemArray
将行的内容作为新的ItemArray返回。因此,当您更改值时,您只需在数组中更改它,而不是在行中更改它。请参阅 DataRow.ItemArray属性(System.Data) [ ^ ]。
The reference row.ItemArray
returns the contents of the row as a new ItemArray. So when you change the value you only change it in the array, not in the row. See DataRow.ItemArray Property (System.Data)[^].
试试这个:
Try this:
foreach (DataRow row in dtSource.Rows)
{
if (row[17].ToString().Length > 32)
{
string ss= "text";
row[17] = ss;
}
}
请参阅: DataRow类(System.Data) [ ^ ]
这篇关于在C#中为数据行分配新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文