如何在 DataRow 对象中设置字段

How to set fields in DataRow object

我已将遗留代码从 vb 迁移到 C#。以下迁移的代码行引发错误:

using System.Data;
..
..
DataRow targetDataRow = targetDataTable.NewRow();
targetDataRow.SetField("DateValue", calendarRow.DateValue.ToShortDateString);

错误:

=Error  CS1061  'DataRow' does not contain a definition for 'SetField' and no accessible extension method 'SetField' accepting a first argument of type 'DataRow' could be found (are you missing a using directive or an assembly reference?)

我知道我可以像这样使用它:

targetDataRow["DateValue"] = calendarRow.DateValue.ToShortDateString);

但是,这是将值设置为 DataRow 对象中的字段的最佳做法吗?

The following migrated line of code raises an error:

 Error  CS1061  'DataRow' does not contain a definition for 'SetField' and no accessible extension method 'SetField' accepting a first argument of type 'DataRow' could be found (are you missing a using directive or an assembly reference?)

此错误是因为您试图使用您在项目中未引用的 DataRowExtension.SetField Method

要添加它,您可以使用 PM 控制台:

 Install-Package System.Data.DataSetExtensions -Version 4.5.0-preview1-26216-02

Is this the best practice to set the value to a field in DataRow object

这种情况下是设置还是调用方法都没有关系。在引擎盖下 SetField 正在做完全相同的事情,仅此而已。