DataRow.Item[] 属性

定义

获取或设置指定列中存储的数据。

重载

Item[DataColumn]

获取或设置指定 DataColumn 中存储的数据。

Item[Int32]

获取或设置由索引指定的列中存储的数据。

Item[String]

获取或设置由名称指定的列中存储的数据。

Item[DataColumn, DataRowVersion]

获取指定 DataColumn 中存储的数据的指定版本。

Item[Int32, DataRowVersion]

获取由索引和要检索的数据版本指定的列中存储的数据。

Item[String, DataRowVersion]

获取指定列中存储的数据的指定版本。

Item[DataColumn]

获取或设置指定 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

属性值

包该数据的 Object

例外

该列不属于此表。

column 为 null。

尝试对已删除的行设置值。

值与列的数据类型不匹配。

示例

以下示例演示如何使用 Item[] 属性获取和设置特定列索引的值。 第一个示例获取用户在控件中单击的任何行中的第一 DataGrid 列的值。 第二个设置作为参数传递给 方法的值。

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 发生异常,则会生成异常。

如果这是即时编辑,请参阅 EndEdit ,了解可以生成的异常。

适用于

Item[Int32]

获取或设置由索引指定的列中存储的数据。

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

列的从零开始的索引。

属性值

包该数据的 Object

例外

尝试对已删除的行设置值时发生。

columnIndex 参数超出范围。

设置值且新值的 TypeDataType 不匹配时发生。

示例

以下示例演示如何使用 Item[] 属性获取和设置特定列索引的值。 第一个示例获取用户在控件中单击的任何行中的第一 DataGrid 列的值。

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 发生异常,则会生成异常。

如果这是一个编辑,请参阅 EndEdit ,了解可以生成的异常。

适用于

Item[String]

获取或设置由名称指定的列中存储的数据。

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

列的名称。

属性值

包该数据的 Object

例外

找不到由 columnName 指定的列。

尝试对已删除的行设置值时发生。

设置值且该值的 TypeDataType 不匹配时发生。

尝试在 AllowDBNull 设置为 false 的列中插入 null 值时发生。

示例

以下示例演示如何使用 Item[] 属性获取和设置特定列索引的值。 第一个示例获取用户在控件中单击的任何行中的第一 DataGrid 列的值。 第二个设置作为参数传递给 方法的值。

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 发生异常,则会生成异常。

如果这是即时编辑,请参阅 EndEdit ,了解可以生成的异常。

适用于

Item[DataColumn, DataRowVersion]

获取指定 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,包含有关该列的信息。

version
DataRowVersion

DataRowVersion 值之一,用于指定需要的行版本。 可能值为 DefaultOriginalCurrentProposed

属性值

包该数据的 Object

例外

该列不属于此表。

column 参数包含 null。

该行没有此版本的数据。

示例

以下示例获取控件中单击的单元格的 DataGrid 当前值。

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

注解

version不应将 与 RowState 属性混淆。 参数 version 描述列包含的数据相对于列的原始值的状态。

设置 属性时,如果 事件中 ColumnChanging 发生异常,则会生成异常。

如果这是即时编辑,请参阅 EndEdit ,了解可以生成的异常。

另请参阅

适用于

Item[Int32, DataRowVersion]

获取由索引和要检索的数据版本指定的列中存储的数据。

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

列的从零开始的索引。

version
DataRowVersion

DataRowVersion 值之一,用于指定需要的行版本。 可能值为 DefaultOriginalCurrentProposed

属性值

包该数据的 Object

例外

columnIndex 参数超出范围。

值与列的数据类型不匹配。

该行没有此版本的数据。

尝试对已删除的行设置值。

示例

以下示例通过 Item[] 对象的 属性获取列的 DataRow 当前值。

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 必须调用 方法才能提交编辑。 调用 EndEdit 方法后,在调用 AcceptChanges 方法之前,将存储原始值和新建议值的内部表示形式。 因此,在调用 AcceptChanges之前,可以使用 version 参数指定所需的列值的哪个版本(或 DataRowVersion.OriginalDataRowVersion.Proposed)。 但是,一旦调用 AcceptChanges 方法,列的版本就会还原为 DataRowVersion.Original。 如果该行是新的,还可以传递 DataRowVersion.Default 参数以检索列的默认值。 传递 DataRowVersion.Current时, 属性将返回当前值,无论其版本如何。

备注

BeginEdit当您更改数据绑定控件的值或DataRow将对象添加到 DataRowCollection时,将隐式调用 方法;EndEdit调用以下方法时,将隐式调用 方法:AcceptChanges对象的 方法DataRowAcceptChanges对象的 方法DataTableCancelEdit 方法。

相比之下,枚举CurrentDataRowVersion调用 方法后EndEdit返回数据版本。

不应将 version 参数与 RowState 属性混淆。 参数 version 描述列包含的数据相对于列的原始值的状态。 属性 RowState 描述相对于其父 DataTable的整个行的状态。

设置 属性时,如果 事件中 ColumnChanging 发生异常,则会生成异常。

如果这是即时编辑,请参阅 EndEdit ,了解可以生成的异常。

适用于

Item[String, DataRowVersion]

获取指定列中存储的数据的指定版本。

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

列的名称。

version
DataRowVersion

DataRowVersion 值之一,用于指定需要的行版本。 可能值为 DefaultOriginalCurrentProposed

属性值

包该数据的 Object

例外

找不到由 columnName 指定的列。

值与列的数据类型不匹配。

该行没有此版本的数据。

该行已删除。

示例

以下示例在控件的单击单元格 DataGrid 中获取当前版本的数据。

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 属性混淆。 参数 version 描述列包含的数据相对于列的原始值的状态。 属性 RowState 描述相对于其父 DataTable的整个行的状态。

设置 属性时,如果 事件中 ColumnChanging 发生异常,则会生成异常。

如果这是即时编辑,请参阅 EndEdit ,了解可以生成的异常。

另请参阅

适用于