How to work in disconnected Mode..
Working with Row State Information
As mentioned earlier in the chapter and illustrated in the section entitled "Deleting and Removing Rows," the
DataRow class defines a property called
RowState that is a
DataRowState enumeration with the values shown in Table 6-2.
Table 6-2. DataRowState Enumeration Values
| Member Name |
Description |
| Added |
The DataRow object has been added to the DataRowCollection object, but DataRow::AcceptChanges has not been called. |
| Deleted |
The DataRow object—belonging to a DataRowCollection—has been deleted using the DataRow::Delete method. |
| Detached |
Either the DataRow object has not been added to the collection or it has been removed via either the DataRowCollection::Remove or DataRowCollection::RemoveAt method. |
| Modified |
The DataRow object—belonging to a DataRowCollection—has been edited, but DataRow::AcceptChanges has not been called. |
| Unchanged |
The DataRow object—belonging to a DataRowCollection—has not changed since the last time DataRow::AcceptChanges was called. |
As you can you can see, these enumeration values cover the various states of a
DataRow. In fact, you can use the Visual Studio .NET debugger with the
EmployeeMaintenance demo application and easily see the various states of the respective
DataRow objects as you add, edit, and delete employee records. The following example illustrates the manipulation of a
DataRow object and the impact of those changes on the object's
RowState property. (As you've already seen examples of connecting to and reading existing data into
DataSet objects, this code also illustrates how to manually define your own
DataSet and
DataTable objects from application data. It's not something you'll use every day, but it's definitely useful at times.)
void PrintRowState(String* currentAction, DataRow* row)
{
Console::WriteLine(String::Format(S"{0,-20}\t\tDataRowState::{1}",
currentAction,
__box(row->RowState)));
}
void CreateAndQueryRowStates()
{
#pragma push_macro("new")
#undef new
try
{
// Define a DataSet
DataSet* dataset = new DataSet();
DataTable* table = new DataTable(S"Players");
dataset->Tables->Add(table);
table->Columns->Add(S"ID", __typeof(Guid));
table->Columns->Add(S"FirstName", __typeof(String));
table->Columns->Add(S"LastName", __typeof(String));
table->Columns->Add(S"Nickname", __typeof(String));
// Create new row
DataRow* row = table->NewRow();
row->Item[S"ID"] = Guid::NewGuid().ToString();
row->Item[S"FirstName"] = S"Yao";
row->Item[S"LastName"] = S"Ming";
row->Item[S"Nickname"] = S"";
PrintRowState(S"Created new row", row);
// Add row table->Rows->Add(row);
PrintRowState(S"Added new row", row);
// Accept changes row->AcceptChanges();
PrintRowState(S"Accepted changes", row);
// Edit row
row->Item[S"Nickname"] = S"Dynasty";
PrintRowState(S"Edited row", row);
// Delete row
row->Delete();
PrintRowState(S"Deleted row", row);
}
catch(Exception* e)
{
Console::WriteLine(e->Message);
}
#pragma pop_macro("new")
}
Plugging these two functions into a Win32 console application with
Managed Extensions support and the appropriate using directives
would result in the following output:
Created new row DataRowState::Detached
Added new row DataRowState::Added
Accepted changes DataRowState::Unchanged
Edited row DataRowState::Modified
Deleted row DataRowState::Deleted
While most of this is what you'd expect after seeing the
DataRowState enumeration descriptions in Table 6-2, there are a couple of things of note. First, a newly created
DataRow has its
RowState set to
Detached until it is added to the
DataRowCollection via the
DataRowcollection::Add method. At that point, the
RowState is initialized to
Added until the
DataRow::AcceptChanges method is called, at which point the
RowState becomes
Unchanged. Finally, note that if you call
DataRow::Delete, the row's
RowState becomes
Deleted, and subsequently calling
DataRowCollection::Remove (or
DataRowCollection::RemoveAt) will not affect its
RowState. The opposite is also true: Removing a
DataRow from a
DataRowCollection sets the row's
RowState to
Detached, but subsequently deleting it will not alter its
RowState value.
Comments
Post a Comment