Export (0) Print
Expand All

DataRow.Item Property (String)

Gets or sets the data stored in the column specified by name.

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

'Declaration
Public Property Item ( _
	columnName As String _
) As Object

Parameters

columnName
Type: System.String

The name of the column.

Property Value

Type: System.Object
An Object that contains the data.

ExceptionCondition
ArgumentException

The column specified by columnName cannot be found.

DeletedRowInaccessibleException

Occurs when you try to set a value on a deleted row.

InvalidCastException

Occurs when you set a value and its Type does not match DataType.

NoNullAllowedException

Occurs when you try to insert a null value into a column where AllowDBNull is set to false.

When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

If this is an immediate edit, see EndEdit for the exceptions that can be generated.

The following examples demonstrate the use of the Item property to get and set the value of a specific column index. The first example gets the value of the first column in any row that a user clicks in a DataGrid control. The second sets a value passed as an argument to the method.

Private Sub DataGrid1_Click( _
    sender As Object, e As System.EventArgs)

    ' Get the DataTable the grid is bound to. 
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = _
        CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        table.Rows(thisGrid.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow("FirstName"))
    ' You can also use the index: 
    ' Console.WriteLine(currentRow(1).ToString()) 
End Sub 

Private Sub SetDataRowValue( _
    grid As DataGrid, newValue As Object)
    ' Set the value of the first column in  
    ' the last row of a DataGrid. 
    Dim table As DataTable = _
        CType(grid.DataSource, DataTable)
    Dim row As DataRow
    row = table.Rows((table.Rows.Count - 1))
    row("FirstName") = newValue
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft