DataRow.Item[] 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置存储在指定列中的数据。
重载
| 名称 | 说明 |
|---|---|
| Item[DataColumn] |
获取或设置存储在指定 DataColumn中的数据。 |
| Item[Int32] |
获取或设置存储在索引指定的列中的数据。 |
| Item[String] |
获取或设置按名称指定的列中存储的数据。 |
| Item[DataColumn, DataRowVersion] |
获取指定的数据存储在指定版本中的指定 DataColumn版本。 |
| Item[Int32, DataRowVersion] |
获取存储在列中的数据,由要检索的数据的索引和版本指定。 |
| Item[String, DataRowVersion] |
获取存储在命名列中的指定数据版本。 |
Item[DataColumn]
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
获取或设置存储在指定 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
包含数据的 A 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]
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
获取或设置存储在索引指定的列中的数据。
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 的范围不足。
示例
以下示例演示如何使用 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]
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
获取或设置按名称指定的列中存储的数据。
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 该列指定的列。
尝试对已删除的行设置值时发生。
尝试将 null 值插入到设置为AllowDBNull的false列中时发生。
示例
以下示例演示如何使用 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]
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
获取指定的数据存储在指定版本中的指定 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
包含有关列的信息的 A DataColumn 。
- version
- DataRowVersion
指定 DataRowVersion 所需行版本的值之一。 可能值为 Default、Original、Current 和 Proposed。
属性值
包含数据的一个 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]
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
获取存储在列中的数据,由要检索的数据的索引和版本指定。
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 所需行版本的值之一。 可能值为 Default、Original、Current 和 Proposed。
属性值
包含数据的一个 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 该方法之前,将存储原始值和新建议值的内部表示形式。 因此,在调用 DataRowVersion.Original。 如果行为新行,还可以传递 DataRowVersion.Default 参数以检索列的默认值。 传递 DataRowVersion.Current时,该属性将返回当前值,无论其版本是什么。
注释
BeginEdit在更改数据绑定控件的值或DataRow将对象添加到DataRowCollectionEndEdit对象时,将隐式调用该方法;调用以下方法时将隐式调用该方法:AcceptChanges对象的方法、DataRow对象的方法AcceptChangesDataTable或CancelEdit方法。
相比之下, DataRowVersion 枚举 Current 在调用方法后 EndEdit 返回数据的版本。
不应将 version 参数与 RowState 属性混淆。 该 version 参数描述列相对于列的原始值包含的数据的状态。 该 RowState 属性描述相对于其父 DataTable行的整个行的状态。
设置属性时,如果事件中 ColumnChanging 发生异常,则会生成异常。
如果这是立即编辑,请参阅 EndEdit 可生成的异常。
适用于
Item[String, DataRowVersion]
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
- Source:
- DataRow.cs
获取存储在命名列中的指定数据版本。
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 所需行版本的值之一。 可能值为 Default、Original、Current 和 Proposed。
属性值
包含数据的一个 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 可生成的异常。