語言

DataRow.Item[] 屬性

定義

取得或設定儲存在指定欄位的資料。

多載

名稱 Description
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
來源:
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 A,裡面包含了資料。

屬性值

一個 Object 包含資料的那個。

例外狀況

該欄位不屬於此表。

是 column 零。

嘗試在刪除的一列上設定值。

該值與欄位的資料型別不相符。

範例

以下範例展示了如何使用該 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
來源:
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 論點超出範圍。

當你設定了值,但新值 Type 與 不符 DataType時會發生。

範例

以下範例展示了如何使用該 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
來源:
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 欄位。

當你嘗試在刪除的列上設定值時會發生。

當你設定一個值,但 Type 值不匹配 DataType時會發生。

當你嘗試在欄位中插入空值,而欄位 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
來源:
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 A 包含關於該欄的資訊。

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]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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 數值是用來指定你想要的列版本。 可能的值為 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 ,會儲存原始與新提出值的內部表示。 因此,在你呼叫 之前 AcceptChanges,你可以用參數 version 來指定你需要的欄位值版本,可以是 或 DataRowVersion.OriginalDataRowVersion.Proposed。 然而,一旦你呼叫該方法, AcceptChanges 欄位的版本就會回復為 DataRowVersion.Original。 如果該列是新的,你也可以傳遞 DataRowVersion.Default 參數以取得該欄的預設值。 當 傳遞 DataRowVersion.Current時,該性質會返回當前值,無論其版本為何。

Note

BeginEdit當你改變資料綁定控制項的值或DataRow將物件加入 時DataRowCollection,該方法會隱含地被呼叫;EndEdit當你呼叫以下方法時,該方法是隱含地被呼叫:AcceptChanges物件的方法DataRow、AcceptChanges物件的方法DataTable,或方法CancelEdit。

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

這個 version 論點不應與 RowState 財產混淆。 這個 version 參數描述了欄位所包含的資料相對於欄位原始值的狀態。 該 RowState 性質描述整列相對於其父 DataTable節點的狀態。

當你設定該屬性時,如果事件中 ColumnChanging 發生例外,就會產生一個例外。

如果是立即編輯,請查看 EndEdit 可產生的例外情況。

適用於

Item[String, DataRowVersion]

來源:
DataRow.cs
來源:
DataRow.cs
來源:
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 數值是用來指定你想要的列版本。 可能的值為 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 可產生的例外情況。

另請參閱

適用於