Export (0) Print
Expand All
2 out of 4 rated this helpful - Rate this topic

DataRowView Class

Represents a customized view of a DataRow exposed as a fully featured Windows Forms control.

For a list of all members of this type, see DataRowView Members.

System.Object
   System.Data.DataRowView

[Visual Basic]
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

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

Remarks

Whenever data is displayed (for example in a DataGrid control), only one version of each row can be displayed. The displayed row is a DataRowView.

A DataRowView can have one of four different version states: Default, Original, Current, and Proposed.

After invoking BeginEdit on a DataRow, any edited value becomes the Proposed value. Until either CancelEdit or EndEdit is invoked, the row has an Original and a Proposed version. If CancelEdit is invoked, the proposed version is discarded, and the value reverts to the original. If EndEdit is invoked, the DataRowView no longer has a Proposed version; instead, the proposed value becomes the current value. Default values are available only on rows that have columns with default values defined.

Example

[Visual Basic, C#, C++] The following example creates a DataTable with two columns, and adds one row to the table. Various versions of the row are then printed. For example, BeginEdit is invoked, the row values are changed, and Original, Proposed, and Current values are printed.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

DataRowView Members | System.Data Namespace | DataRow

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.