}
To test this application, you can create a Windows application and write the
code in listing 9-16 on the form load or a button-click event handler. As you
can see from listing 9-16, the code creates a DataView object, adds a new row to
DataView, and then removes the first row from DataView. The adding and removing
of rows is responsible for firing the OnListChanged event handler.
Listing 9-16. Adding, updating, and deleting rows of a DataView
private void
Form1_load(object sender, System.EventArgs
e)
{
OleDbConnection
conn = new OleDbConnection();
string strDSN =
"provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source= C:/Northwind.mdb";
conn.ConnectionString = strDSN;
string sql =
"SELECT EmployeeID, LastName, FirstName FROM
Employees";
// Opening Connection
conn.Open();
// Create a data Adapter
OleDbDataAdapter
da = new OleDbDataAdapter(sql, conn);
// Create and fill DataSet
DataSet ds = new
DataSet();
da.Fill(ds,
"Employees");
DataView dv = ds.Tables["Employees"].DefaultView;
// Add DataView Event Handlers
dv.ListChanged +=
new System.ComponentModel.ListChangedEventHandler
(OnListChanged);
// Add a row to the DataView
dv.AllowEdit =
true;
DataRowView rw = dv.AddNew();
rw.BeginEdit();
rw["FirstName"]
= "FName";
rw["LastName"]
= "LName";
rw.EndEdit();
// Remove a row from the DataView
if (dv.Count > 0)
{
dv.Delete(0);
dv[0].Row.AcceptChanges();
}
// Close the connection
conn.Close();
}