7.15 DataTable Events

The following section describes DataTable events.

7.15.1 ColumnChanged and ColumnChanging

The ColumnChanged and ColumnChanging events can be handled to validate data or control user interface elements. The ColumnChanging event is raised when a value is being changed in a specified column; the ColumnChanged event is raised after the value in the column has been changed. Both events pass a DataColumnChangeEventArgs argument to the event handler that provide information specific to the event.

The following code demonstrates handling the ColumnChanging and ColumnChanged events to perform data validation and logging:

 DataTable dt = new DataTable(); dt.ColumnChanged += new DataColumnChangeEventHandler(dt_ColumnChanged); dt.ColumnChanging += new DataColumnChangeEventHandler(dt_ColumnChanging); private static void dt_ColumnChanging(object sender,     DataColumnChangeEventArgs e); {     if (e.Column.ColumnName == "MyColumn")     {         if(e.ProposedValue.Equals("Invalid Data")         {             e.Row.RowError = "Invalid data.";             e.Row.SetColumnError(e.Column, "Column value " +                 "cannot be " e.ProposedValue.ToString());             }     } } private static void ds_ColumnChanged(object sender,     DataColumnChangeEventArgs e); {     System.IO.TextWriter tw = System.IO.File.AppendText("colchange.log");     tw.WriteLine("ColumnChanging: Name = {0}; ProposedValue = {1}; " +         "Row Id = {2}", e.ColumnName, e.ProposedValue.ToString(),         e.Row["Id"].ToString());     tw.Close();  } 

7.15.2 RowChanged, RowChanging, RowDeleted, and RowDeleting

The DataTable raises four events in response to actions performed on rows. These events are RowChanging and RowChanged, which are raised, respectively, before and after a row is edited; RowDeleting and RowDeleted are raised, respectively, before and after a row is marked for deletion. These events can support custom validation logic similar to the ColumnChanging and ColumnChanged events described earlier. All four events pass a DataRowChangeEventArgs argument to the event handler providing information specific to the event.

The following code demonstrates handling the RowChanged , RowChanging , RowDeleted , and RowDeleting events:

 DataTable dt = new DataTable(); dt.RowChanged+= new DataRowChangeEventHandler(dt_RowChanged); dt.RowChanging+= new DataRowChangeEventHandler(dt_RowChanging); dt.RowDeleted+= new DataRowChangeEventHandler(dt_RowDeleted); dt.RowDeleting+= new DataRowChangeEventHandler(dt_RowDeleting); private void dt_RowChanged(object sender, DataRowChangeEventArgs e); {     MessageBox.Show("RowChanged: Action  =  " + e.Action + "; " +         "Row Id  =  " + e.Row["Id"].ToString()); } private void dt_RowChanging(object sender, DataRowChangeEventArgs e); {     MessageBox.Show("RowChanging: Action  =  " + e.Action + "; " +         "Row Id  =  " + e.Row["Id"].ToString()); } private void dt_RowDeleted(object sender, DataRowChangeEventArgs e); {     MessageBox.Show("RowDeleted: Action  =  " + e.Action + "; " +         "Row Id  =  " + e.Row["Id"].ToString()); } private void dt_RowDeleting(object sender, DataRowChangeEventArgs e); {     MessageBox.Show("RowDeleting: Action  =  " + e.Action + "; " +         "Row Id  =  " + e.Row["Id"].ToString()); } 


ADO. NET in a Nutshell
ADO.NET in a Nutshell
ISBN: 0596003617
EAN: 2147483647
Year: 2005
Pages: 415

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net