.NET Framework Class Library
DataRow..::.Item Property (Int32)

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

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

Visual Basic (Declaration)
Public Property Item ( _
    columnIndex As Integer _
) As Object
Visual Basic (Usage)
Dim instance As DataRow
Dim columnIndex As Integer
Dim value As Object

value = instance.Item(columnIndex)

instance.Item(columnIndex) = value
C#
public Object this[
    int columnIndex
] { get; set; }
Visual C++
public:
property Object^ Item[int columnIndex] {
    Object^ get (int columnIndex);
    void set (int columnIndex, Object^ value);
}
JScript
JScript does not support indexed properties.

Parameters

columnIndex
Type: System..::.Int32
The zero-based index of the column.

Property Value

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

ExceptionCondition
DeletedRowInaccessibleException

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

IndexOutOfRangeException

The columnIndex argument is out of range.

InvalidCastException

Occurs when you set the value and the new value's Type does not match DataType.

Remarks

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

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

Examples

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.

Visual Basic
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal 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(1))
    ' You can also use the name of the column:
    ' Console.WriteLine(currentRow("FirstName"))
    End Sub

    Private Sub SetDataRowValue( _
        ByVal grid As DataGrid, ByVal newValue As Object)

    ' Set the value of the last column in the last row of a DataGrid.
    Dim table As DataTable
    table = CType(grid.DataSource, DataTable)
    Dim row As DataRow 
    row = table.Rows(table.Rows.Count-1)
    row(table.Columns.Count-1) = newValue
End Sub
C#
private void DataGrid1_Click(object sender, 
    System.EventArgs e)
{
    // Get the DataTable the grid is bound to.
    DataGrid thisGrid = (DataGrid) sender;
    DataTable table = (DataTable) thisGrid.DataSource;
    DataRow currentRow = 
        table.Rows[thisGrid.CurrentCell.RowNumber];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow[1]);
    // You can also use the name of the column:
    // Console.WriteLine(currentRow["FirstName"])
}

private void SetDataRowValue(DataGrid grid, object newValue)
{
    // Set the value of the last column in the last row of a DataGrid.
    DataTable table;
    table = (DataTable) grid.DataSource;
    DataRow row;

    // Get last row
    row = (DataRow)table.Rows[table.Rows.Count-1];

    // Set value of last column
    row[table.Columns.Count-1] = newValue;
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Tags :


Page view tracker