DataRow.Item[] 屬性

定義

取得或設定儲存於指定資料行的資料。

多載

Item[DataColumn]

取得或設定儲存於指定的 DataColumn 中的資料。

Item[Int32]

取得或設定儲存於索引指定的資料行中的資料。

Item[String]

取得或設定儲存於名稱所指定之資料行的資料。

Item[DataColumn, DataRowVersion]

取得儲存於指定的 DataColumn 中指定版本的資料。

Item[Int32, DataRowVersion]

取得儲存於資料行中的資料,它是由要擷取的資料索引和版本所指定。

Item[String, DataRowVersion]

取得儲存於具名資料行中指定版本的資料。

Item[DataColumn]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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

包含資料的 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]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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 引數超出範圍。

設定了值而新值的 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]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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 指定的資料行。

嘗試在刪除的資料列上設定值時發生。

當設定值而其 TypeDataType 不相符時發生。

發生於您嘗試將 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]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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

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]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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 值,指定您所要的資料列版本。 可能的值為 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當您變更數據綁定控件的值或加入 DataRowCollection物件時DataRow,會隱含呼叫 方法;EndEdit當您呼叫下列方法時,會隱含呼叫 方法:DataRowAcceptChanges物件的 方法、AcceptChangesDataTable物件的 方法或 CancelEdit 方法。

相較之下, DataRowVersion 列舉 Current 會在呼叫 方法之後 EndEdit 傳回數據的版本。

version 變數不應與 RowState 屬性混淆。 自 version 變數描述數據行相對於數據行原始值所包含的數據狀態。 屬性 RowState 描述相對於其父 DataTable系 的整個數據列狀態。

當您設定 屬性時,如果在事件中 ColumnChanging 發生例外狀況,就會產生例外狀況。

如果這是立即編輯,請參閱 EndEdit 以取得可產生的例外狀況。

適用於

Item[String, DataRowVersion]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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 值,指定您所要的資料列版本。 可能的值為 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 以取得可產生的例外狀況。

另請參閱

適用於