DataRowView クラス
完全な機能を持つ Windows フォーム コントロールとして公開されている DataRow のカスタマイズされたビューを表します。
この型のすべてのメンバの一覧については、DataRowView メンバ を参照してください。
System.Object
System.Data.DataRowView
Public Class DataRowView
Implements ICustomTypeDescriptor, IEditableObject, _
IDataErrorInfo
[C#]
public class DataRowView : ICustomTypeDescriptor, IEditableObject,
IDataErrorInfo
[C++]
public __gc class DataRowView : public ICustomTypeDescriptor,
IEditableObject, IDataErrorInfo
[JScript]
public class DataRowView implements ICustomTypeDescriptor,
IEditableObject, IDataErrorInfo
スレッドセーフ
この型は、マルチスレッド読み取り操作に対して安全です。すべての書き込み操作の同期をとる必要があります。
解説
データを (DataGrid コントロールなどで) 表示する場合は、1 行につき 1 つのバージョンだけを表示できます。表示されている行は DataRowView です。
DataRowView は、Default、Original、Current、および Proposed という 4 種類のバージョン状態のいずれか 1 つをとることができます。
DataRow で BeginEdit を呼び出した後で編集した値は Proposed 値になります。 CancelEdit または EndEdit のどちらかを呼び出すまでは、行には Original バージョンと Proposed バージョンが格納されています。CancelEdit を呼び出すと、提示されたバージョンが破棄され、値が元の値に戻ります。EndEdit を呼び出すと、DataRowView には Proposed バージョンがなくなり、提示された値が現在の値になります。既定値は、既定値が定義されている列を含む行でだけ使用できます。
使用例
[Visual Basic, C#, C++] 2 つの列で DataTable を作成し、1 つの行をこのテーブルに追加する例を次に示します。行をテーブルに追加すると、行の各種のバージョンが出力されます。たとえば、 BeginEdit が呼び出され、行の値が変更され、Original、Proposed、Current の各値が出力されます。
Private Sub DemonstrateRowVersions()
' Create a DataTable.
Dim myTable As New DataTable("myTable")
' Create two columns. Set DataType and DefaultValue properties.
Dim col1 As New DataColumn("col1")
col1.DataType = System.Type.GetType("System.String")
col1.DefaultValue = "myDefaultValue"
Dim col2 As New DataColumn("col2")
col2.DataType = System.Type.GetType("System.Decimal")
col2.DefaultValue = 0.1
' Add Columns to the DataTable.
myTable.Columns.Add(col1)
myTable.Columns.Add(col2)
Dim newRow As DataRow
' Create one row and add it to the DataRow collection.
newRow = myTable.NewRow()
newRow(col1) = "Nitrogen"
newRow(col2) = 33
myTable.Rows.Add(newRow)
' Invoke AcceptChanges on the table.
myTable.AcceptChanges()
' Print the current values:
PrintRowVersion(myTable, DataRowVersion.Current)
' Change values in the table.
myTable.Rows(0).BeginEdit()
myTable.Rows(0)("col1") = "New Component"
myTable.Rows(0)("col2") = 100
' Print the proposed values.
If myTable.Rows(0).HasVersion(DataRowVersion.Proposed) Then
Console.WriteLine _
(myTable.Rows(0)("col1", DataRowVersion.Proposed).ToString())
Console.WriteLine _
(myTable.Rows(0)("col2", DataRowVersion.Proposed).ToString())
End If
' Print the original values.
If myTable.Rows(0).HasVersion(DataRowVersion.Original) Then
Console.WriteLine _
(myTable.Rows(0)("col1", DataRowVersion.Original).ToString())
Console.WriteLine _
(myTable.Rows(0)("col2", DataRowVersion.Original).ToString())
End If
' Cancel the edit.
myTable.Rows(0).CancelEdit()
' Print the Current values.
If myTable.Rows(0).HasVersion(DataRowVersion.Current) Then
Console.WriteLine _
(myTable.Rows(0)("col1", DataRowVersion.Current).ToString())
Console.WriteLine _
(myTable.Rows(0)("col2", DataRowVersion.Current).ToString())
End If
' Try to print the proposed values again--gone after cancel edit.
' Print the proposed values.
If myTable.Rows(0).HasVersion(DataRowVersion.Proposed) Then
Console.WriteLine _
(myTable.Rows(0)("col1", DataRowVersion.Proposed).ToString())
Console.WriteLine _
(myTable.Rows(0)("col2", DataRowVersion.Proposed).ToString())
End If
' Change the values again.
myTable.Rows(0).BeginEdit()
myTable.Rows(0)("col1") = "New Value"
myTable.Rows(0)("col2") = 100.1
' Print the proposed values.
PrintRowVersion(myTable, DataRowVersion.Proposed)
' End the edit.
myTable.Rows(0).EndEdit()
PrintRowVersion(myTable, DataRowVersion.Current)
PrintRowVersion(myTable, DataRowVersion.Proposed)
' Add a new row.
newRow = myTable.NewRow()
myTable.Rows.Add(newRow)
' Print default rows.
PrintRowVersion(myTable, DataRowVersion.Default)
' Try to print original rows--there is only one row.
PrintRowVersion(myTable, DataRowVersion.Original)
' Try to print proposed version--there are no rows.
PrintRowVersion(myTable, DataRowVersion.Proposed)
' Print the current version--two rows.
PrintRowVersion(myTable, DataRowVersion.Current)
' Invoke AcceptChanges on the new row.
myTable.Rows(1).AcceptChanges()
' Print the original rows--two are now available.
PrintRowVersion(myTable, DataRowVersion.Original)
End Sub 'DemonstrateRowVersions
Private Sub PrintRowVersion(dt As DataTable, ver As DataRowVersion)
' Print a new line and the version using the ReturnRowVersion function.
Console.WriteLine(ControlChars.Cr + ver.ToString() + " Version:")
' Print the value of each column in each row.
Try
Dim row As DataRow
For Each row In dt.Rows
' Print the specified version of the row's value.
Console.Write(row(0, ver).ToString() + ", ")
Console.Write(row(1, ver).ToString() + ControlChars.Cr)
Next row
Catch e As Exception
' Process exception and return.
Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
log.Source = "My Application"
log.WriteEntry(e.ToString())
Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
End Try
End Sub
[C#]
private void DemonstrateRowVersions(){
// Create a DataTable.
DataTable myTable = new DataTable("myTable");
// Create two columns. Set DataType and DefaultValue properties.
DataColumn col1 = new DataColumn("col1");
col1.DataType=System.Type.GetType("System.String");
col1.DefaultValue= "myDefaultValue";
DataColumn col2 = new DataColumn("col2");
col2.DataType=System.Type.GetType("System.Decimal");
col2.DefaultValue=.1;
// Add Columns to the DataTable.
myTable.Columns.Add (col1);
myTable.Columns.Add(col2);
DataRow newRow;
// Create one row and add it to the DataRow collection.
newRow = myTable.NewRow();
newRow[col1]="Nitrogen";
newRow[col2]=33;
myTable.Rows.Add(newRow);
// Invoke AcceptChanges on the table.
myTable.AcceptChanges();
// Print the current values:
PrintRowVersion(myTable, DataRowVersion.Current);
// Change values in the table.
myTable.Rows[0].BeginEdit();
myTable.Rows[0]["col1"]= "New Component";
myTable.Rows[0]["col2"]= 100;
// Print the proposed values.
if(myTable.Rows[0].HasVersion(DataRowVersion.Proposed)){
Console.WriteLine(myTable.Rows[0]["col1", DataRowVersion.Proposed]);
Console.WriteLine(myTable.Rows[0]["col2", DataRowVersion.Proposed]);
}
// Print the original values.
if(myTable.Rows[0].HasVersion(DataRowVersion.Original)){
Console.WriteLine(myTable.Rows[0]["col1", DataRowVersion.Original]);
Console.WriteLine(myTable.Rows[0]["col2", DataRowVersion.Original]);
}
// Cancel the edit.
myTable.Rows[0].CancelEdit();
// Print the Current values.
if(myTable.Rows[0].HasVersion(DataRowVersion.Current)){
Console.WriteLine(myTable.Rows[0]["col1", DataRowVersion.Current]);
Console.WriteLine(myTable.Rows[0]["col2", DataRowVersion.Current]);
}
// Try to print the proposed values again--gone after cancel edit.
// Print the proposed values.
if(myTable.Rows[0].HasVersion(DataRowVersion.Proposed)){
Console.WriteLine(myTable.Rows[0]["col1", DataRowVersion.Proposed]);
Console.WriteLine(myTable.Rows[0]["col2", DataRowVersion.Proposed]);
}
// Change the values again.
myTable.Rows[0].BeginEdit();
myTable.Rows[0]["col1"]="New Value";
myTable.Rows[0]["col2"]=100.1;
// Print the proposed values.
PrintRowVersion(myTable, DataRowVersion.Proposed);
// End the edit.
myTable.Rows[0].EndEdit();
PrintRowVersion(myTable, DataRowVersion.Current);
PrintRowVersion(myTable, DataRowVersion.Proposed);
// Add a new row.
newRow = myTable.NewRow();
myTable.Rows.Add(newRow);
// Print default rows.
PrintRowVersion(myTable, DataRowVersion.Default);
// Try to print original rows--there is only one row.
PrintRowVersion(myTable, DataRowVersion.Original);
// Try to print proposed version--there are no rows.
PrintRowVersion(myTable, DataRowVersion.Proposed);
// Print the current version--two rows.
PrintRowVersion(myTable, DataRowVersion.Current);
// Invoke AcceptChanges on the new row.
myTable.Rows[1].AcceptChanges();
// Print the original rows--two are now available.
PrintRowVersion(myTable, DataRowVersion.Original);
}
private void PrintRowVersion(DataTable dt, DataRowVersion ver){
// Print a new line and the version using the ReturnRowVersion function.
Console.WriteLine("\n" + ver.ToString() + " Version:");
// Print the value of each column in each row.
try{
foreach(DataRow row in dt.Rows ) {
// Print the specified version of the row's value.
Console.Write(row[0,ver] + ", ");
Console.Write(row[1,ver] + "\n");
}
}
catch(Exception e) {
// Process exception and return.
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = "My Application";
log.WriteEntry(e.ToString());
Console.WriteLine("Exception of type {0} occurred.", e.GetType());
}
}
[C++]
private:
void DemonstrateRowVersions(){
// Create a DataTable.
DataTable* myTable = new DataTable(S"myTable");
// Create two columns. Set DataType and DefaultValue properties.
DataColumn* col1 = new DataColumn(S"col1");
col1->DataType=System::Type::GetType(S"System.String");
col1->DefaultValue= S"myDefaultValue";
DataColumn* col2 = new DataColumn(S"col2");
col2->DataType=System::Type::GetType(S"System.Decimal");
col2->DefaultValue=__box(.1);
// Add Columns to the DataTable.
myTable->Columns->Add (col1);
myTable->Columns->Add(col2);
DataRow* newRow;
// Create one row and add it to the DataRow collection.
newRow = myTable->NewRow();
newRow->Item[col1]=S"Nitrogen";
newRow->Item[col2]=__box(33);
myTable->Rows->Add(newRow);
// Invoke AcceptChanges on the table.
myTable->AcceptChanges();
// Print the current values:
PrintRowVersion(myTable, DataRowVersion::Current);
// Change values in the table.
myTable->Rows->Item[0]->BeginEdit();
myTable->Rows->Item[0]->Item[S"col1"]= S"New Component";
myTable->Rows->Item[0]->Item[S"col2"]= __box(100);
// Print the proposed values.
if(myTable->Rows->Item[0]->HasVersion(DataRowVersion::Proposed)){
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col1", DataRowVersion::Proposed]);
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col2", DataRowVersion::Proposed]);
}
// Print the original values.
if(myTable->Rows->Item[0]->HasVersion(DataRowVersion::Original)){
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col1", DataRowVersion::Original]);
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col2", DataRowVersion::Original]);
}
// Cancel the edit.
myTable->Rows->Item[0]->CancelEdit();
// Print the Current values.
if(myTable->Rows->Item[0]->HasVersion(DataRowVersion::Current)){
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col1", DataRowVersion::Current]);
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col2", DataRowVersion::Current]);
}
// Try to print the proposed values again--gone after cancel edit.
// Print the proposed values.
if(myTable->Rows->Item[0]->HasVersion(DataRowVersion::Proposed)){
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col1", DataRowVersion::Proposed]);
Console::WriteLine(myTable->Rows->Item[0]->Item[S"col2", DataRowVersion::Proposed]);
}
// Change the values again.
myTable->Rows->Item[0]->BeginEdit();
myTable->Rows->Item[0]->Item[S"col1"]=S"New Value";
myTable->Rows->Item[0]->Item[S"col2"]=__box(100.1);
// Print the proposed values.
PrintRowVersion(myTable, DataRowVersion::Proposed);
// End the edit.
myTable->Rows->Item[0]->EndEdit();
PrintRowVersion(myTable, DataRowVersion::Current);
PrintRowVersion(myTable, DataRowVersion::Proposed);
// Add a new row.
newRow = myTable->NewRow();
myTable->Rows->Add(newRow);
// Print default rows.
PrintRowVersion(myTable, DataRowVersion::Default);
// Try to print original rows--there is only one row.
PrintRowVersion(myTable, DataRowVersion::Original);
// Try to print proposed version--there are no rows.
PrintRowVersion(myTable, DataRowVersion::Proposed);
// Print the current version--two rows.
PrintRowVersion(myTable, DataRowVersion::Current);
// Invoke AcceptChanges on the new row.
myTable->Rows->Item[1]->AcceptChanges();
// Print the original rows--two are now available.
PrintRowVersion(myTable, DataRowVersion::Original);
}
void PrintRowVersion(DataTable* dt, DataRowVersion ver){
// Print a new line and the version using the ReturnRowVersion function.
Console::WriteLine(S"\n{0} Version:", __box(ver));
// Print the value of each column in each row.
try{
System::Collections::IEnumerator* myEnum = dt->Rows->GetEnumerator();
while (myEnum->MoveNext())
{
DataRow* row = __try_cast<DataRow*>(myEnum->Current);
// Print the specified version of the row's value.
Console::Write(S"{0}, ", row->Item[0,ver]);
Console::Write(S"{0}\n", row->Item[1,ver]);
}
}
catch(Exception* e) {
// Process exception and return.
System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
log->Source = S"My Application";
log->WriteEntry(e->ToString());
Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Data
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: System.Data (System.Data.dll 内)