DataRow.Item[String, DataRowVersion] DataRow.Item[String, DataRowVersion] DataRow.Item[String, DataRowVersion] DataRow.Item[String, DataRowVersion] Property

定义

重载

Item[DataColumn] Item[DataColumn] Item[DataColumn] Item[DataColumn]

获取或设置指定 DataColumn 中存储的数据。Gets or sets the data stored in the specified DataColumn.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取或设置由索引指定的列中存储的数据。Gets or sets the data stored in the column specified by index.

Item[String] Item[String] Item[String] Item[String]

获取或设置由名称指定的列中存储的数据。Gets or sets the data stored in the column specified by name.

Item[DataColumn, DataRowVersion] Item[DataColumn, DataRowVersion] Item[DataColumn, DataRowVersion] Item[DataColumn, DataRowVersion]

获取指定 DataColumn 中存储的数据的指定版本。Gets the specified version of data stored in the specified DataColumn.

Item[Int32, DataRowVersion] Item[Int32, DataRowVersion] Item[Int32, DataRowVersion] Item[Int32, DataRowVersion]

获取由索引和要检索的数据版本指定的列中存储的数据。Gets the data stored in the column, specified by index and version of the data to retrieve.

Item[String, DataRowVersion] Item[String, DataRowVersion] Item[String, DataRowVersion] Item[String, DataRowVersion]

获取指定列中存储的数据的指定版本。Gets the specified version of data stored in the named column.

Item[DataColumn] Item[DataColumn] Item[DataColumn] Item[DataColumn]

获取或设置指定 DataColumn 中存储的数据。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

参数

column
DataColumn DataColumn DataColumn DataColumn

一个包含数据的 DataColumnA DataColumn that contains the data.

属性值

包该数据的 ObjectAn Object that contains the data.

异常

该列不属于此表。The column does not belong to this table.

尝试对已删除的行设置值。An attempt was made to set a value on a deleted row.

值与列的数据类型不匹配。The data types of the value and the column do not match.

示例

下面的示例演示如何使用Item[String, DataRowVersion]属性来获取和设置特定列索引的值。The following examples demonstrate the use of the Item[String, DataRowVersion] property to get and set the value of a specific column index. 第一个示例获取用户在DataGrid控件中单击的任意行中的第一列的值。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

注解

设置属性时, 如果ColumnChanging事件中发生异常, 则会生成异常。When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

如果这是立即编辑, 请参阅EndEdit , 了解可生成的异常。If this is an immediate edit, see EndEdit for the exceptions that can be generated.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取或设置由索引指定的列中存储的数据。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

参数

columnIndex
Int32 Int32 Int32 Int32

列的从零开始的索引。The zero-based index of the column.

属性值

包该数据的 ObjectAn Object that contains the data.

异常

尝试对已删除的行设置值时发生。Occurs when you try to set a value on a deleted row.

columnIndex 参数超出范围。The columnIndex argument is out of range.

设置值且新值的 TypeDataType 不匹配时发生。Occurs when you set the value and the new value's Type does not match DataType.

示例

下面的示例演示如何使用Item[String, DataRowVersion]属性来获取和设置特定列索引的值。The following examples demonstrate the use of the Item[String, DataRowVersion] property to get and set the value of a specific column index. 第一个示例获取用户在DataGrid控件中单击的任意行中的第一列的值。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

注解

设置属性时, 如果ColumnChanging事件中发生异常, 则会生成异常。When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

如果这是一个编辑, 请EndEdit参阅了解可生成的异常。If this is an edit, see EndEdit for the exceptions that can be generated.

Item[String] Item[String] Item[String] Item[String]

获取或设置由名称指定的列中存储的数据。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

参数

columnName
String String String String

列的名称。The name of the column.

属性值

包该数据的 ObjectAn Object that contains the data.

异常

找不到由 columnName 指定的列。The column specified by columnName cannot be found.

尝试对已删除的行设置值时发生。Occurs when you try to set a value on a deleted row.

设置值且该值的 TypeDataType 不匹配时发生。Occurs when you set a value and its Type does not match DataType.

尝试在 AllowDBNull 设置为 false 的列中插入 null 值时发生。Occurs when you try to insert a null value into a column where AllowDBNull is set to false.

示例

下面的示例演示如何使用Item[String, DataRowVersion]属性来获取和设置特定列索引的值。The following examples demonstrate the use of the Item[String, DataRowVersion] property to get and set the value of a specific column index. 第一个示例获取用户在DataGrid控件中单击的任意行中的第一列的值。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

注解

设置属性时, 如果ColumnChanging事件中发生异常, 则会生成异常。When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

如果这是立即编辑, 请参阅EndEdit , 了解可生成的异常。If this is an immediate edit, see EndEdit for the exceptions that can be generated.

Item[DataColumn, DataRowVersion] Item[DataColumn, DataRowVersion] Item[DataColumn, DataRowVersion] Item[DataColumn, DataRowVersion]

获取指定 DataColumn 中存储的数据的指定版本。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

参数

column
DataColumn DataColumn DataColumn DataColumn

DataColumn,包含有关该列的信息。A DataColumn that contains information about the column.

version
DataRowVersion DataRowVersion DataRowVersion DataRowVersion

DataRowVersion 值之一,用于指定需要的行版本。One of the DataRowVersion values that specifies the row version that you want. 可能值为 DefaultOriginalCurrentProposedPossible values are Default, Original, Current, and Proposed.

属性值

包该数据的 ObjectAn Object that contains the data.

异常

该列不属于此表。The column does not belong to the table.

column 参数包含 null。The column argument contains null.

该行没有此版本的数据。The row does not have this version of data.

示例

下面的示例获取DataGrid控件中单击的单元格的当前值。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

注解

versionRowState与属性混淆。The version should not be confused with the RowState property. version参数说明列所包含的数据相对于列的原始值的状态。The version argument describes the state of the data that is contained by the column relative to the column's original value.

设置属性时, 如果ColumnChanging事件中发生异常, 则会生成异常。When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

如果这是立即编辑, 请参阅EndEdit , 了解可生成的异常。If this is an immediate edit, see EndEdit for the exceptions that can be generated.

另请参阅

Item[Int32, DataRowVersion] Item[Int32, DataRowVersion] Item[Int32, DataRowVersion] Item[Int32, DataRowVersion]

获取由索引和要检索的数据版本指定的列中存储的数据。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

参数

columnIndex
Int32 Int32 Int32 Int32

列的从零开始的索引。The zero-based index of the column.

version
DataRowVersion DataRowVersion DataRowVersion DataRowVersion

DataRowVersion 值之一,用于指定需要的行版本。One of the DataRowVersion values that specifies the row version that you want. 可能值为 DefaultOriginalCurrentProposedPossible values are Default, Original, Current, and Proposed.

属性值

包该数据的 ObjectAn Object that contains the data.

异常

columnIndex 参数超出范围。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.

示例

下面的示例通过Item[String, DataRowVersion] DataRow对象的属性获取列的当前值。The following example gets the current value of a column through the Item[String, DataRowVersion] 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

注解

在调用BeginEdit方法后EndEdit , 只能创建或更新行; 同样, 必须调用方法才能提交编辑。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. 调用EndEdit方法后, 在AcceptChanges调用方法之前, 会存储原始值和新的建议值的内部表示形式。After you call the EndEdit method, and before you call the AcceptChanges method, internal representations of the original and new proposed values are stored. AcceptChanges因此, 在调用之前, 可以version使用参数指定DataRowVersion.Original所需的列值的版本 (或DataRowVersion.Proposed)。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. 但是, 一旦调用AcceptChanges方法, 列的版本就会恢复为。 DataRowVersion.OriginalHowever, as soon as you call the AcceptChanges method, the version of the column reverts to DataRowVersion.Original. 如果行是新行, 则还可以传递DataRowVersion.Default参数来检索列的默认值。If the row is new, you can also pass DataRowVersion.Default for the parameter to retrieve the column's default value. 传递DataRowVersion.Current时, 属性将返回当前值, 无论其版本是什么。When passing DataRowVersion.Current, the property returns the current value, whatever its version may be.

备注

EndEdit DataRow DataRowCollection当您更改数据绑定控件的值或将对象添加到时, 将隐式调用AcceptChanges 方法;调用以下方法时,将隐式调用方法:BeginEdit对象的方法AcceptChangesDataTable对象的方法或CancelEdit方法。 DataRowThe 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.

DataRowVersion此相反, 枚举Current在调用EndEdit方法后返回数据的版本。By contrast, the DataRowVersion enumeration Current returns the version of the data after the EndEdit method has been called.

不应将RowState参数与属性混淆。 versionThe version argument should not be confused with the RowState property. version参数说明列所包含的数据相对于列的原始值的状态。The version argument describes the state of the data that is contained by the column relative to the column's original value. 属性描述相对于其父级DataTable的整行的状态。 RowStateThe RowState property describes the state of the whole row relative to its parent DataTable.

设置属性时, 如果ColumnChanging事件中发生异常, 则会生成异常。When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

如果这是立即编辑, 请参阅EndEdit , 了解可生成的异常。If this is an immediate edit, see EndEdit for the exceptions that can be generated.

Item[String, DataRowVersion] Item[String, DataRowVersion] Item[String, DataRowVersion] Item[String, DataRowVersion]

获取指定列中存储的数据的指定版本。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

参数

columnName
String String String String

列的名称。The name of the column.

version
DataRowVersion DataRowVersion DataRowVersion DataRowVersion

DataRowVersion 值之一,用于指定需要的行版本。One of the DataRowVersion values that specifies the row version that you want. 可能值为 DefaultOriginalCurrentProposedPossible values are Default, Original, Current, and Proposed.

属性值

包该数据的 ObjectAn Object that contains the data.

异常

找不到由 columnName 指定的列。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.

示例

下面的示例获取DataGrid控件中单击的单元格的当前版本的数据。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

注解

不应将版本与RowState属性混淆。The version should not be confused with the RowState property. version参数说明列所包含的数据相对于列的原始值的状态。The version argument describes the state of the data that is contained by the column relative to the column's original value. 属性描述相对于其父级DataTable的整行的状态。 RowStateThe RowState property describes the state of the whole row relative to its parent DataTable.

设置属性时, 如果ColumnChanging事件中发生异常, 则会生成异常。When you set the property, an exception is generated if an exception occurs in the ColumnChanging event.

如果这是立即编辑, 请参阅EndEdit , 了解可生成的异常。If this is an immediate edit, see EndEdit for the exceptions that can be generated.

另请参阅

适用于