This documentation is archived and is not being maintained.

DataRow.Item Property (String)

.NET Framework 1.1

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

[C#] In C#, this property is the indexer for the DataRow class.

[Visual Basic]
Overloads Public Default Property Item( _
   ByVal columnName As String _
) As Object
[C#]
public object this[
 string columnName
] {get; set;}
[C++]
public: __property Object* get_Item(
 String* columnName
);
public: __property void set_Item(
 String* columnName,
   Object*
);
[JScript]
returnValue = DataRowObject.Item(columnName);
DataRowObject.Item(columnName) = returnValue;
-or-
returnValue = DataRowObject(columnName);
DataRowObject(columnName) = returnValue;

[JScript] In JScript, you can use the default indexed properties defined by a type, but you cannot explicitly define your own. However, specifying the expando attribute on a class automatically provides a default indexed property whose type is Object and whose index type is String.

Arguments [JScript]

columnName
The name of the column.

Parameters [Visual Basic, C#, C++]

columnName
The name of the column.

Property Value

An Object that contains the data.

Exceptions

Exception Type Condition
ArgumentException The column specified by columnName cannot be found.
DeletedRowInaccessibleException Occurs when attempting to set a value on a deleted row.
InvalidCastException Occurs when setting a value and its Type doesn't match DataType.

Remarks

When setting 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.

Example

[Visual Basic, C#, C++] The following examples demonstrate the use of the Item property to get and set the value of a given 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.

[Visual Basic] 
Private Sub DataGrid1_Click(sender As Object, e As System.EventArgs)
    Dim currRow As DataRow
    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim myTable As DataTable = CType(thisGrid.DataSource, DataTable)
    currRow = myTable.Rows(thisGrid.CurrentCell.RowNumber)
    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currRow("FirstName"))
   ' You can also use the index:
   ' Console.WriteLine(currRow(1).ToString())
End Sub
   
Private Sub SetDataRowValue(myGrid As DataGrid, newValue As Object)
    ' Set the value of the first column in the last row of a DataGrid.
    Dim myTable As DataTable
    myTable = CType(myGrid.DataSource, DataTable)
    Dim myRow As DataRow
    myRow = myTable.Rows((myTable.Rows.Count - 1))
    myRow("FirstName") = newValue
End Sub

[C#] 
private void DataGrid1_Click(object sender, System.EventArgs e){
   DataRow currRow;
   // Get the DataTable the grid is bound to.
   DataGrid thisGrid = (DataGrid) sender;
   DataTable myTable = (DataTable) thisGrid.DataSource;
   currRow = myTable.Rows[thisGrid.CurrentCell.RowNumber];
   // Get the value of the column 1 in the DataTable.
   Console.WriteLine(currRow["FirstName"]);
   // You can also use the index:
   // Console.WriteLine(currRow[1]);
}

private void SetDataRowValue(DataGrid myGrid, object newValue){
   // Set the value of the first column in the last row of a DataGrid.
   DataTable myTable;
   myTable = (DataTable) myGrid.DataSource;
   DataRow myRow; 
   myRow = myTable.Rows[myTable.Rows.Count-1];
   myRow["FirstName"] = newValue;
}

[C++] 
private:
 void DataGrid1_Click(Object* sender, System::EventArgs* /*e*/){
    DataRow* currRow;
    // Get the DataTable the grid is bound to.
    DataGrid* thisGrid = dynamic_cast<DataGrid*> (sender);
    DataTable* myTable = dynamic_cast<DataTable*> (thisGrid->DataSource);
    currRow = myTable->Rows->Item[thisGrid->CurrentCell.RowNumber];
    // Get the value of the column 1 in the DataTable.
    Console::WriteLine(currRow->Item[S"FirstName"]);
    // You can also use the index:
    // Console.WriteLine(currRow[1]);
 }
 
 void SetDataRowValue(DataGrid* myGrid, Object* newValue){
    // Set the value of the first column in the last row of a DataGrid.
    DataTable* myTable;
    myTable = dynamic_cast<DataTable*> (myGrid->DataSource);
    DataRow* myRow; 
    myRow = myTable->Rows->Item[myTable->Rows->Count-1];
    myRow->Item[S"FirstName"] = newValue;
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DataRow Class | DataRow Members | System.Data Namespace | DataRow.Item Overload List

Show: