Gets or sets the data stored at the named column.
[C#] In C#, this member 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 Item property whose type is Object and whose index type is String.
An Object that contains the data.
Exception Type | Condition |
---|---|
IndexOutOfRangeException | The column specified by columnName can't be found. |
InvalidCastException | Occurs when setting a value and its 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. The second sets a value passed as an argument to the method.
[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. currRow = DataGrid1.DataGridTable.DataTable. _ Rows(DataGrid1.CurrentCell.RowNumber) ' Get the value of the column 1 in the DataTable. label1.Text = CurrRow("FirstName") End Sub Private Sub SetDataRowValue(ByVal myGrid As DataGrid, ByVal newVal As Object) ' Set the value of a column in the last row of a DataGrid. Dim t As DataTable t = mygrid.DataGridTable.DataTable Dim myRow As DataRow myRow = t.Rows(t.Rows.Count - 1) myRow("FirstName") = newVal End Sub
DataRow Class | DataRow Members | System.Data Namespace | DataRow.Item Overload List