Home | Contact Us
<< Back to C-SHARP
C# DataTable RowChanged Example: AcceptChangesUse the DataTable type and its RowChanged and AcceptChanges event handlers.
DataTable RowChanged. The DataTable type is intricate. It provides many methods, properties and event handlers. The RowChanged event handler helps us detect when something changes in a DataTable. There are some secrets to its use.
Example. This example program creates a DataTable with four columns and five rows. It then hooks up the RowChanged event handler. To add RowChanged, type the table.RowChanged part 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.
Caution: You cannot change a DataRow within RowChanged. This is prohibited. In RowChanged we must check values or call other methods.
Output: 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.
C# program that uses RowChanged, DataTable
static void Main()
DataTable table = new DataTable();
// ... Unique id.
table.Columns["Id"].Unique = true;
// ... 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");
// ... Handle RowChanged.
table.RowChanged += table_RowChanged;
// ... Change a row.
table.Rows["Dosage"] = 50;
table.Rows["Dosage"] = -1;
// ... Pause.
table.Rows["Dosage"] = 15;
table.Rows["Dosage"] = 800;
static void table_RowChanged(object sender, DataRowChangeEventArgs e)
Console.WriteLine("... Changed: " + (int)e.Row["Dosage"]);
... Changed: 50
... Changed: -1
... Changed: 15
... Changed: 800
AcceptChanges. One tricky aspect to using DataTable and monitoring for changes is calling AcceptChanges. In my testing, it is best to call AcceptChanges after adding rows. Do not call it excessively or changes may keep reappearing.
Do not do this. 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 StackOverflowException will occur.StackOverflowException
Discussion. Sometimes there is an action that you need to take when a row changes. The RowChanged event handler may be helpful here. This will often be specific to the program you are working on.
Tip: We can use RejectChanges and AcceptChanges to process changes to the DataTable contents.
And: The RowChanged and RowChanging events may be helpful in this style of code. ColumnChanged and ColumnChanging are also available.Event
Summary. Code that handles DataTables tends to be complex. And it is often application-specific, tied to a certain database schema and platform. Events like RowChanged, and methods like AcceptChanges are sometimes helpful in monitoring mutations.DataTable