Export (0) Print
Expand All

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)

public Object this [
	int columnIndex
] { get; set; 
/** @property */
public Object get_Item (int columnIndex)

/** @property */
public void set_Item (int columnIndex, Object value)

JScript supports the use of indexed properties, but not the declaration of new ones.

Parameters

columnIndex

The zero-based index of the column.

Property Value

An Object that contains the data.

Exception typeCondition

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.

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.

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.

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;


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft