Home
C#
DataTable RowChanged Example
Updated Dec 24, 2024
Dot Net Perls
DataTable RowChanged. DataTable provides many methods, properties and event handlers. The RowChanged event handler helps us detect when something changes in a DataTable.
DataTable
DataRow
Some notes. Sometimes there is an action that you need to take when a row changes in the DataTable. The RowChanged event handler may be helpful here.
event
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.
Result 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.
using System; using System.Data; class Program { static void Main() { DataTable table = new DataTable(); // ... Unique id. table.Columns.Add("Id", typeof(int)); table.Columns["Id"].Unique = true; table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patient", typeof(string)); // ... Five DataRows. table.Rows.Add(1, 25, "Evista", "Lundberg"); table.Rows.Add(2, 50, "Adcirca", "Jeffries"); table.Rows.Add(3, 10, "Glucagon", "Johnson"); table.Rows.Add(4, 20, "Amyvid", "Skinner"); table.Rows.Add(5, 80, "Alimta", "Nguyen"); table.AcceptChanges(); // ... Handle RowChanged. table.RowChanged += table_RowChanged; // ... Change a row. table.Rows[0]["Dosage"] = 50; table.Rows[0]["Dosage"] = -1; // ... Pause. Console.WriteLine("[Press enter]"); Console.ReadLine(); table.Rows[0]["Dosage"] = 15; table.Rows[1]["Dosage"] = 800; } static void table_RowChanged(object sender, DataRowChangeEventArgs e) { Console.WriteLine("... Changed: " + (int)e.Row["Dosage"]); } }
... Changed: 50 ... Changed: -1 [Press enter] ... Changed: 15 ... Changed: 800
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.
StackOverflowException
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.
DataColumn
Also AcceptChanges can help when monitoring for changes. In my testing, it is best to call AcceptChanges after adding rows.
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (simplify).
Home
Changes
© 2007-2025 Sam Allen