DataTable RowChanged. DataTable provides many methods, properties and event handlers. The RowChanged event handler helps us detect when something changes in a DataTable.
This program creates a DataTable with 4 columns and 5 rows. It then hooks up the RowChanged event handler. To add RowChanged, type table.RowChanged and then the += characters.
Then Allow Visual Studio to create a method: press tab twice. You can edit the contents of that method to perform nearly any action.
Warning You cannot change a DataRow within RowChanged. This is prohibited. In RowChanged we must check values or call other methods.
Detail In the output, we see that table_RowChanged is triggered four times. It is called each time a cell in a row changes.
Info We changed the "Dosage" cell repeatedly on the rows. We print this in table_RowChanged.
AcceptChanges. One trick to using DataTable and monitoring for changes is calling AcceptChanges. In my testing, it is best to call AcceptChanges after adding rows.
StackOverflowException. If you try to change a cell in the RowChanged event handler, an infinite loop may be entered. Each change will trigger another call to RowChanged.
A discussion. We can use RejectChanges and AcceptChanges to process changes to the DataTable contents. The RowChanged and RowChanging events may be helpful.
And ColumnChanged and ColumnChanging are also available. These detect when a DataColumn has been modified.
A summary. Code that handles DataTables tends to be complex and application-specific. Events like RowChanged, and methods like AcceptChanges are help in monitoring mutations.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 27, 2022 (edit).