Tài liệu Adding, Modifying, and Removing DataRowView Objects from a DataView - Pdf 92


Adding, Modifying, and Removing DataRowView Objects from a DataView
It's important to understand that DataRowView objects in a DataView provide access to
the underlying DataRow objects in a DataTable. Therefore, when you examine and edit
the contents of a DataRowView, you're actually working with the underlying DataRow.
Similarly, when you remove a DataRowView, you are removing the underlying
DataRow.
Adding a DataRowView to a DataView
To add a new DataRowView to a DataView, you call the AddNew() method of your
DataView. The AddNew() method returns a DataRowView object that you use to set the
column values for the new row.The following example calls the AddNew() method of the
customersDV DataView:
DataRowView customerDRV = customersDV.AddNew();
customerDRV["CustomerID"] = "J7COM";
customerDRV["CompanyName"] = "J7 Company";
customerDRV["Country"] = "UK";
customerDRV.EndEdit();
Notice the use of the EndEdit() method of the customerDRV DataRowView to end the
editing. The EndEdit() method creates a new DataRow in the underlying DataTable. The
DataColumn objects in the new DataRow will contain the column values specified in the
previous code.

Note You can undo the addition by calling the CancelEdit() method of a DataRowView.
You can get the underlying DataRow added to the DataTable using the Row property of a
DataRowView. For example:
DataRow customerDR = customerDRV.Row;
Modifying an Existing DataRowView
To begin modifying an existing DataRowView in a DataView, you call the BeginEdit()
method of the DataRowView in your DataView. The following example calls the
BeginEdit() method for the first DataRowView in customersDV:
customersDV[0].BeginEdit();

Note You can call the RejectChanges() method of a DataTable to undo the deletions.
This method will also undo any uncommitted additions and modifications of rows.
Listing 13.3
shows a program that adds, modifies, and removes DataRowView objects
from a DataView. This program also displays the IsNew and IsEdit properties of the
DataRowView objects, which indicate whether the DataRowView is new and is being
edited.
Listing 13.3: ADDMODIFYANDREMOVEDATAROWVIEWS.CS

/*
AddModifyAndRemoveDataRowViews.cs illustrates how to
add, modify, and remove DataRowView objects from a DataView
*/

using System;
using System.Data;
using System.Data.SqlClient;

class AddModifyAndRemoveDataRowViews
{
public static void DisplayDataRow(
DataRow myDataRow,
DataTable myDataTable
)
{
Console.WriteLine("\nIn DisplayDataRow()");
foreach (DataColumn myDataColumn in myDataTable.Columns)
{
Console.WriteLine(myDataColumn + "= " +
myDataRow[myDataColumn]);

Console.WriteLine("\nCalling customersDV.AddNew()");
DataRowView customerDRV = customersDV.AddNew();
customerDRV["CustomerID"] = "J7COM";
customerDRV["CompanyName"] = "J7 Company";
customerDRV["Country"] = "UK";
Console.WriteLine("customerDRV[\" CustomerID\"] = " +
customerDRV["CustomerID"]);
Console.WriteLine("customerDRV[\" CompanyName\"] = " +
customerDRV["CompanyName"]);
Console.WriteLine("customerDRV[\" Country\"] = " +
customerDRV["Country"]);
Console.WriteLine("customerDRV.IsNew = " + customerDRV.IsNew);
Console.WriteLine("customerDRV.IsEdit = " + customerDRV.IsEdit);
customerDRV.EndEdit();
// get and display the underlying DataRow
DataRow customerDR = customerDRV.Row;
DisplayDataRow(customerDR, customersDT);

// modify the CompanyName of customerDRV
Console.WriteLine("\nSetting customersDV[0][\" CompanyName\"] to Widgets Inc.");
customersDV[0].BeginEdit();
customersDV[0]["CompanyName"] = "Widgets Inc.";
Console.WriteLine("customersDV[0][\" CustomerID\"] = " +
customersDV[0]["CustomerID"]);
Console.WriteLine("customersDV[0][\" CompanyName\"] = " +
customersDV[0]["CompanyName"]);
Console.WriteLine("customersDV[0].IsNew = " + customersDV[0].IsNew);
Console.WriteLine("customersDV[0].IsEdit = " + customersDV[0].IsEdit);
customersDV[0].EndEdit();


}
}
}

The output from this program is as follows:
Calling customersDV.AddNew()
customerDRV["CustomerID"] = J7COM
customerDRV["CompanyName"] = J7 Company
customerDRV["Country"] = UK
customerDRV.IsNew = True
customerDRV.IsEdit = True

In DisplayDataRow()
CustomerID = J7COM
CompanyName = J7 Company


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status