简体   繁体   English

如何遍历数据表并设置值?

[英]How to iterate through a datatable and set values?

How would I go about doing something similar to the code below? 我将如何做与下面的代码类似的事情? I'd like to iterate through my datatable and set / change values. 我想遍历我的数据表并设置/更改值。 In this case, setting all of the rows DateRcvd to the current date. 在这种情况下,请将所有行DateRcvd设置为当前日期。

foreach(DataRow row in SubVwr.Tables[0].Tbl.Rows)
{
    row.Field<DateTime>("DateRcvd") = DateTime.Today;
}

You can iterate through each row and use an indexer property of the DataRow : 您可以遍历每一行并使用DataRow的indexer属性:

foreach(DataRow row in SubVwr.Tables[0].Tbl.Rows)
{
   row["DateRcvd"] = DateTime.Today;
}

The modern way of writing it is: 现代的写法是:

row.SetField("DateRcvd", DateTime.Today);

Since SetField is a generic method, it won't box your value type like using the default indexer will (the indexer takes an object ). 由于SetField是通用方法,因此不会像使用默认索引器那样将值类型装箱(索引器接受object )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM