DataRow.Item[] Property

Definition

Gets or sets data stored in a specified column.

Overloads

Item[DataColumn]

Gets or sets the data stored in the specified DataColumn.

Item[Int32]

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

Item[String]

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

Item[DataColumn, DataRowVersion]

Gets the specified version of data stored in the specified DataColumn.

Item[Int32, DataRowVersion]

Gets the data stored in the column, specified by index and version of the data to retrieve.

Item[String, DataRowVersion]

Gets the specified version of data stored in the named column.

Item[DataColumn]

Source:
DataRow.cs
Source:
DataRow.cs
Source:
DataRow.cs

Gets or sets the data stored in the specified DataColumn.

C#
public object this[System.Data.DataColumn column] { get; set; }

Parameters

column
DataColumn

A DataColumn that contains the data.

Property Value

An Object that contains the data.

Exceptions

The column does not belong to this table.

The column is null.

An attempt was made to set a value on a deleted row.

The data types of the value and the column do not match.

Examples

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.

VB
Private Sub DataGrid1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs)
    
    Dim dataGridTable As DataTable = _
        CType(DataGrid1.DataSource, DataTable)
    ' Set the current row using the RowNumber 
    ' property of the CurrentCell.
    Dim currentRow As DataRow = _
        dataGridTable.Rows(DataGrid1.CurrentCell.RowNumber)
    Dim column As DataColumn = dataGridTable.Columns(1)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(column).ToString()
End Sub
 
Private Sub SetDataRowValue( _
    ByVal grid As DataGrid, ByVal newVal As Object)

    ' Set the value of a column in the last row of a DataGrid.
    Dim table As DataTable = CType(grid.DataSource, DataTable)
    Dim row As DataRow = table.Rows(table.Rows.Count - 1)
    Dim column As DataColumn = table.Columns("FirstName")
    row(column)= newVal
End Sub

Remarks

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[Int32]

Source:
DataRow.cs
Source:
DataRow.cs
Source:
DataRow.cs

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

C#
public object this[int columnIndex] { get; set; }

Parameters

columnIndex
Int32

The zero-based index of the column.

Property Value

An Object that contains the data.

Exceptions

Occurs when you try to set a value on a deleted row.

The columnIndex argument is out of range.

Occurs when you set the value and the new value's Type does not match DataType.

Examples

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.

C#
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;
}

Remarks

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[String]

Source:
DataRow.cs
Source:
DataRow.cs
Source:
DataRow.cs

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

C#
public object this[string columnName] { get; set; }

Parameters

columnName
String

The name of the column.

Property Value

An Object that contains the data.

Exceptions

The column specified by columnName cannot be found.

Occurs when you try to set a value on a deleted row.

Occurs when you set a value and its Type does not match DataType.

Occurs when you try to insert a null value into a column where AllowDBNull is set to false.

Examples

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.

C#
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;
}

Remarks

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[DataColumn, DataRowVersion]

Source:
DataRow.cs
Source:
DataRow.cs
Source:
DataRow.cs

Gets the specified version of data stored in the specified DataColumn.

C#
public object this[System.Data.DataColumn column, System.Data.DataRowVersion version] { get; }

Parameters

column
DataColumn

A DataColumn that contains information about the column.

version
DataRowVersion

One of the DataRowVersion values that specifies the row version that you want. Possible values are Default, Original, Current, and Proposed.

Property Value

An Object that contains the data.

Exceptions

The column does not belong to the table.

The column argument contains null.

The row does not have this version of data.

Examples

The following example gets the current value of a clicked cell in the DataGrid control.

C#
private void DataGrid1_Click(object sender,
    System.EventArgs e)
{
    DataTable dataGridTable =
        (DataTable)DataGrid1.DataSource;

    // Set the current row using the RowNumber
    // property of the CurrentCell.
    DataRow currentRow = dataGridTable.Rows[DataGrid1.CurrentCell.RowNumber];
    DataColumn column = dataGridTable.Columns[1];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow[column, DataRowVersion.Current]);
}

Remarks

The version should not be confused with the RowState property. The version argument describes the state of the data that is contained by the column relative to the column's original value.

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[Int32, DataRowVersion]

Source:
DataRow.cs
Source:
DataRow.cs
Source:
DataRow.cs

Gets the data stored in the column, specified by index and version of the data to retrieve.

C#
public object this[int columnIndex, System.Data.DataRowVersion version] { get; }

Parameters

columnIndex
Int32

The zero-based index of the column.

version
DataRowVersion

One of the DataRowVersion values that specifies the row version that you want. Possible values are Default, Original, Current, and Proposed.

Property Value

An Object that contains the data.

Exceptions

The columnIndex argument is out of range.

The data types of the value and the column do not match.

The row does not have this version of data.

An attempt was made to set a value on a deleted row.

Examples

The following example gets the current value of a column through the Item[] property of the DataRow object.

VB
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    ' Set the current row using the RowNumber property of the CurrentCell.
    Dim currentRow As DataRow = CType(DataGrid1.DataSource, DataTable). _
       Rows(DataGrid1.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(1, DataRowVersion.Current).ToString()
End Sub

Remarks

You can only create or update a row after you call the BeginEdit method; similarly, the EndEdit method must be called to commit the edit. After you call the EndEdit method, and before you call the AcceptChanges method, internal representations of the original and new proposed values are stored. Therefore, until you call the AcceptChanges, you can use the version argument to specify which version of a column's value you need, either the DataRowVersion.Original or DataRowVersion.Proposed. However, as soon as you call the AcceptChanges method, the version of the column reverts to DataRowVersion.Original. If the row is new, you can also pass DataRowVersion.Default for the parameter to retrieve the column's default value. When passing DataRowVersion.Current, the property returns the current value, whatever its version may be.

Note

The BeginEdit method is called implicitly when you change the value of a data-bound control or when a DataRow object is added to the DataRowCollection; the EndEdit method is called implicitly when you call the following methods: the AcceptChanges method of the DataRow object, the AcceptChanges method of the DataTable object, or the CancelEdit method.

By contrast, the DataRowVersion enumeration Current returns the version of the data after the EndEdit method has been called.

The version argument should not be confused with the RowState property. The version argument describes the state of the data that is contained by the column relative to the column's original value. The RowState property describes the state of the whole row relative to its parent DataTable.

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[String, DataRowVersion]

Source:
DataRow.cs
Source:
DataRow.cs
Source:
DataRow.cs

Gets the specified version of data stored in the named column.

C#
public object this[string columnName, System.Data.DataRowVersion version] { get; }

Parameters

columnName
String

The name of the column.

version
DataRowVersion

One of the DataRowVersion values that specifies the row version that you want. Possible values are Default, Original, Current, and Proposed.

Property Value

An Object that contains the data.

Exceptions

The column specified by columnName cannot be found.

The data types of the value and the column do not match.

The row does not have this version of data.

The row was deleted.

Examples

The following example gets the current version of data at a clicked cell of a DataGrid control.

C#
private void DataGrid1_Click(object sender, System.EventArgs e)
{
    // Set the current row using the RowNumber
    // property of the CurrentCell.
    DataRow currentRow =
        ((DataTable)(DataGrid1.DataSource)).
        Rows[DataGrid1.CurrentCell.RowNumber];

    // Print the current value of the column named "FirstName."
    Console.WriteLine(currentRow["FirstName",
        DataRowVersion.Current]);
}

Remarks

The version should not be confused with the RowState property. The version argument describes the state of the data that is contained by the column relative to the column's original value. The RowState property describes the state of the whole row relative to its parent DataTable.

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1