DataRow.Item Property (String)
Gets or sets the data stored in the column specified by name.
Assembly: System.Data (in System.Data.dll)
Parameters
- columnName
-
Type:
System.String
The name of the column.
| Exception | Condition |
|---|---|
| ArgumentException | The column specified by columnName cannot be found. |
| DeletedRowInaccessibleException | Occurs when you try to set a value on a deleted row. |
| InvalidCastException | |
| NoNullAllowedException | Occurs when you try to insert a null value into a column where AllowDBNull is set to false. |
When you set 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.
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. The second sets a value passed as an argument to the method.
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["FirstName"]); // You can also use the index: // Console.WriteLine(currentRow[1]); } private void SetDataRowValue( DataGrid grid, object newValue) { // Set the value of the first column in // the last row of a DataGrid. DataTable table = (DataTable) grid.DataSource; DataRow row = table.Rows[table.Rows.Count-1]; row["FirstName"] = newValue; }
Available since 1.1