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 Remember that DataRowView objects in a DataView start at index 0, and therefore
customersDV[0] is the first DataRowView in customersDV.
You can then modify a DataColumn in the underlying DataRow through the
DataRowView. The following example sets the CompanyName DataColumn to Widgets
Inc.:
customersDV[0]["CompanyName"] = "Widgets Inc.";
Once you've finished making your modifications, you call the EndEdit() method to make
your modifications permanent in the underlying DataTable. For example:
customersDV[0].EndEdit();
Note You can undo the modification by calling the CancelEdit() method of a
DataRowView.
Removing an Existing DataRowView
To remove an existing DataRowView from a DataView, you can call the Delete() method
of either the DataView or the DataRowView. When calling the Delete() method of a
DataView, you pass the index of the DataRowView you want to remove. The following
example removes the second DataRowView from customersDV:
customersDV.Delete(1);
When calling the Delete() method of a DataRowView, you simply call that method of the
DataRowView in your DataView. The following example removes the third
DataRowView from customersDV:
customersDV[2].Delete();
With either of these Delete() methods, the deletion isn't committed in the underlying
DataTable until you call the AcceptChanges() method of your DataTable. For example:
customersDT.AcceptChanges();
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]);
}
}
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, Country " +
"FROM Customers";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
mySqlDataAdapter.Fill(myDataSet, "Customers");
mySqlConnection.Close();
DataTable customersDT = myDataSet.Tables["Customers"];
// set up the filter expression
string filterExpression = "Country = 'UK'";
// create a DataView object named customersDV
DataView customersDV = new DataView();
customersDV.Table = customersDT;
customersDV.RowFilter = filterExpression;
// add a new DataRowView (adds a DataRow to the DataTable)
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();
// display the underlying DataRow
DisplayDataRow(customersDV[0].Row, customersDT);
// remove the second DataRowView from customersDV
Console.WriteLine("\ncustomersDV[1][\" CustomerID\"] = " +
customersDV[1]["CustomerID"]);
Console.WriteLine("\nCalling customersDV.Delete(1)");
customersDV.Delete(1);
Console.WriteLine("customersDV[1].IsNew = " + customersDV[1].IsNew);
Console.WriteLine("customersDV[1].IsEdit = " + customersDV[1].IsEdit);
// remove the third DataRowView from customersDV
Console.WriteLine("\ncustomersDV[2][\" CustomerID\"] = " +
customersDV[2]["CustomerID"]);
Console.WriteLine("\nCalling customersDV[2].Delete()");
customersDV[2].Delete();
// call the AcceptChanges() method of customersDT to
// make the deletes permanent in customersDT
customersDT.AcceptChanges();
// display the rows in the customersDV DataView object
Console.WriteLine("\nDataRowView objects in customersDV:\n");
foreach (DataRowView myDataRowView in customersDV)
{
for (int count = 0; count < customersDV.Table.Columns.Count; count++)
{
Console.WriteLine(myDataRowView[count]);
}
Console.WriteLine("");
}
}
}
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