DataRowExtensions.Field<T> Method (DataRow, DataColumn, DataRowVersion)

Provides strongly-typed access to each of the column values in the specified row. The Field method also supports nullable types.

Namespace:  System.Data
Assembly:  System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll)

public static T Field<T>(
	this DataRow row,
	DataColumn column,
	DataRowVersion version
)

Type Parameters

T

A generic parameter that specifies the return type of the column.

Parameters

row
Type: System.Data.DataRow

The input DataRow, which acts as the this instance for the extension method.

column
Type: System.Data.DataColumn

The input DataColumn object that specifies the column to return the value of.

version
Type: System.Data.DataRowVersion

A DataRowVersion enumeration that specifies the version of the column value to return, such as Current or Original version.

Return Value

Type: T
The value, of type T, of the DataColumn specified by column and version.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type DataRow. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
InvalidCastException

The value type of the underlying column could not be cast to the type specified by the generic parameter, T.

IndexOutOfRangeException

The column specified by column does not exist in the DataTable that the DataRow is a part of.

NullReferenceException

A null value was assigned to a non-nullable type.

The DataSet class represents null values with the Value instance of the DBNull class. A Language-Integrated Query (LINQ) expression that accessed a column with a null value would generate a InvalidCastException at run time. Additionally, DataSet doesn’t support nullable types. The Field method provides support for accessing columns as nullable types. If the underlying value in the DataSet is Value, the returned nullable type will have a value of null.

If the value of the specified DataColumn is null and T is a reference type or nullable type, then the return type will be null. The Field method will not return Value.

The Field method does not perform type conversions. If type conversion is required, you should first obtain the column value by using the Field method. The column value should then be converted to another type.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft