|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This article has been
excerpted from book "A Programmer's Guide to ADO.NET in C#".
DataView and DataViewManager define the ListChanged event, which occurs when a
row is added to or deleted from a DataView and DataViewManager object. The
ListChangedEventHandler method handles the ListChanged event; it's as follows:
public delegate
void
ListChangedEventHandler(object sender,
ListChangedEventArges e);
Where sender is the source of the event and e is ListChangedEventArgs, which
contains the event data. Table 9-8 defines the ListChangedEventArgs members.
Table 9-8. The ListChangedEventArgs members
MEMBER |
DESCRIPTION |
ListChangedType |
Returns the way that list changed |
NewIndex |
Returns the new index of the item in the list |
OldIndex |
Returns the old index of the item in the list |
Listing 9-15 shows the OnListChanged event handler.
Listing 9-15.The OnListChanged event handler
protected static
void OnListChanged(object
sender, System.ComponentModel.ListChangedEventArgs
args)
{ MessageBox.Show("ListChanged:
Type = " + args.ListChangedType +
", OldIndex = " + args.OldIndex +
", NewIndex = " + args.NewIndex); }
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(); }
CAUTION: As you can see from listing 9-16, the AcceptChanges() method
removes a row permanently from the database. If you don't want to remove the
row, call the RejectChanges() method.
The output of listing 9-16 looks like figure 9-11 and 9-12.
Figure 9-11. The ListChange events output after adding a new row
Figure 9-12. The ListChanged events output after deleting a row
Conclusion
Hope this article would have helped you in understanding
working
with DataView and DataViewManager Events in ADO.NET.
See other articles on
the website also for further reference.
|
This essential guide
to Microsoft's ADO.NET overviews C#, then leads you toward deeper
understanding of ADO.NET. |
|