Breyta

Deila með


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.

public:
 property System::Object ^ default[System::Data::DataColumn ^] { System::Object ^ get(System::Data::DataColumn ^ column); void set(System::Data::DataColumn ^ column, System::Object ^ value); };
public object this[System.Data.DataColumn column] { get; set; }
member this.Item(System.Data.DataColumn) : obj with get, set
Default Public Property Item(column As DataColumn) As Object

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.

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

Item[Int32]

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

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

public:
 property System::Object ^ default[int] { System::Object ^ get(int columnIndex); void set(int columnIndex, System::Object ^ value); };
public object this[int columnIndex] { get; set; }
member this.Item(int) : obj with get, set
Default Public Property Item(columnIndex As Integer) As Object

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.

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;
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)

    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        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"))
    End Sub

    Private Sub SetDataRowValue( _
        ByVal grid As DataGrid, ByVal newValue As Object)

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

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

Item[String]

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

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

public:
 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ columnName); void set(System::String ^ columnName, System::Object ^ value); };
public object this[string columnName] { get; set; }
member this.Item(string) : obj with get, set
Default Public Property Item(columnName As String) As Object

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.

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;
}
Private Sub DataGrid1_Click( _
    sender As Object, e As System.EventArgs)
     
    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = _
        CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        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).ToString())
End Sub
    
Private Sub SetDataRowValue( _
    grid As DataGrid, newValue As Object)
    ' Set the value of the first column in 
    ' the last row of a DataGrid.
    Dim table As DataTable = _
        CType(grid.DataSource, DataTable)
    Dim row As DataRow
    row = table.Rows((table.Rows.Count - 1))
    row("FirstName") = newValue
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

Item[DataColumn, DataRowVersion]

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

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

public:
 property System::Object ^ default[System::Data::DataColumn ^, System::Data::DataRowVersion] { System::Object ^ get(System::Data::DataColumn ^ column, System::Data::DataRowVersion version); };
public object this[System.Data.DataColumn column, System.Data.DataRowVersion version] { get; }
member this.Item(System.Data.DataColumn * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(column As DataColumn, version As DataRowVersion) As Object

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.

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]);
}
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.CurrentRowIndex)
    Dim column As DataColumn = dataGridTable.Columns(1)

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

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

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.

public:
 property System::Object ^ default[int, System::Data::DataRowVersion] { System::Object ^ get(int columnIndex, System::Data::DataRowVersion version); };
public object this[int columnIndex, System.Data.DataRowVersion version] { get; }
member this.Item(int * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(columnIndex As Integer, version As DataRowVersion) As Object

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.

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

Item[String, DataRowVersion]

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

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

public:
 property System::Object ^ default[System::String ^, System::Data::DataRowVersion] { System::Object ^ get(System::String ^ columnName, System::Data::DataRowVersion version); };
public object this[string columnName, System.Data.DataRowVersion version] { get; }
member this.Item(string * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(columnName As String, version As DataRowVersion) As Object

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.

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]);
}
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)

    ' Print the current value of the column named "FirstName."
    Console.WriteLine(currentRow("FirstName", _
        DataRowVersion.Current).ToString())
End Sub

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