Gets or sets the data stored at the specified column index.
[C#] In C#, this member is the indexer for the DataRow class.
[Visual Basic] Overloads Public Default Property Item( _ ByVal columnIndex As Integer _ ) As Object [C#] public object this[ int columnIndex ] {get; set;} [C++] public: __property Object* get_Item( int columnIndex ); public: __property void set_Item( int columnIndex, Object* ); [JScript] returnValue = DataRowObject.Item(columnIndex); DataRowObject.Item(columnIndex) = returnValue; -or- returnValue = DataRowObject(columnIndex); DataRowObject(columnIndex) = 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 Item property whose type is Object and whose index type is String.
An Object that contains the data.
Exception Type | Condition |
---|---|
IndexOutOfRangeException | The columnIndex argument is out of range. |
InvalidCastException | Occurs when setting the value and the new value's Type doesn't match DataType. |
DeletedRowInaccessibleException | Occurs when attempting to set a value on a deleted row. |
When setting the property, an exception will be thrown if an exception occurs in the ColumnChanging event.
If this is an immediate edit, see EndEdit for the exceptions that can be thrown.
The following examples demonstrate the use of the Item property (DataRow indexer) 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 System.WinForms.DataGrid control.
[Visual Basic]
Private Sub DataGrid1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim currRow As DataRow ' Set the current row using the RowNumber property of the CurrentCell. ' Assuming the DataGrid is bound to a DataTable. Dim t As DataTable t = DataGrid1.DataSource currRow = t.Rows(DataGrid1.CurrentCell.RowNumber) ' Get the value of the column 1 in the DataTable. Console.WriteLine(currRow(1)) End Sub Private Sub SetDataRowValue(ByVal myGrid As DataGrid, ByVal newVal As Object) ' Set the value of the last column in the last row of a DataGrid Dim t As DataTable t = mygrid.DataSource.DataTable Dim myRow As DataRow myRow = t.Rows(t.Rows.Count - 1) myRow(t.Columns - 1) = newVal End Sub
DataRow Class | DataRow Members | System.Data Namespace | DataRow.Item Overload List