如何更改DataTabe中的一列内容 [英] How Do I Change One Column Content In DataTabe
问题描述
大家好
C#问题
我有一个具有5列的数据表
第4列是格鲁吉亚日期
如何更改此列的所有内容(列 4 )
并再次保存(再次保存在第4列==更改后的每个数据都放在旧位置)
例如:
我有此数据表:
a b c d e f
a b c d e f
a b c d e f
a b c d e f
将e更改为z数据表后,如下所示:
a b c d z f
a b c d z f
a b c d z f
a b c d z f
我该怎么办?
请帮助我
非常感谢
例如,您可以遍历数据行并修改每行的值.像这样的东西(编译器未检查代码)
foreach (Systems.Data.DataRow行 in mydatatable.Rows中){ row [ 4 ] = " ; } mydatatable.AcceptChanges();
之后,您可以将数据表保存到某处(到文件,数据库等).
答案1是好的.
或者,如果您想使用LINQ
,则可以使用以下代码
data.AsEnumerable().其中(dr = > dr.Field< string>( 4 )== " ) .Select(dr = > dr.SetField< string>( 4 , z")).Count();
其中data
是DataTable
LINQ
已推迟执行,因此在迭代返回的序列或调用诸如Count之类的标量函数之前,它不会执行查询.因此,在上面的代码中调用Count
以立即执行查询.
hi all
C# Problem
i have a data table with 5 columns
column 4 is Georgian Date
how do i change
all content of this column (Column 4)
and save it again (save in column 4 again == each data after changed placed in old place )
for example :
i have this data table :
a b c d e f
a b c d e f
a b c d e f
a b c d e f
after change e to z data table like below :
a b c d z f
a b c d z f
a b c d z f
a b c d z f
how do i ?
Please Help Me
Thanks A Lot
You can for example loop through the data rows and modify the values of each row. Something like this (code not checked with the compiler)
foreach (System.Data.DataRow row in mydatatable.Rows) { row[4] = "Z"; } mydatatable.AcceptChanges();
Afterwards, you can save the datatable somewhere (to a file, database etc).
The answer 1 is good.
As an alternative if you want to useLINQ
then the following code can be used
data.AsEnumerable().Where(dr => dr.Field<string>(4)=="e") .Select(dr => dr.SetField<string>(4,"z")).Count();
Wheredata
is theDataTable
TheLINQ
has deferred execution, so it will not execute the query until the sequence returned is iterated or a scalar function like Count is called. Hence,Count
is called in the above code to immediately execute the query.
这篇关于如何更改DataTabe中的一列内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!