user2432715
user2432715

Reputation: 67

What is the difference between DatatableRowchanging Event and DataTableRowChanged Event

I am trying understand the difference between RowChanging and Rowchanged event of datatable. I have Checked the MSDN blog but could not find any practical difference.

Here is what i got:

  1. Both Use same Datarowchangeeventargs : So they have same set of event data. namely ; e.Action and e.row.

In watch Window i checked if there are any changes to these event data but they were similar. If there would have been Cancel in RowChanging then it would have meant about validating.

So all in all , I m confused why the two event if they meant the same.

Please clear my confusion.

Here's my difference that i found based on the pointers that i got from answers.

I added few rows to column and called both events. I tried doing e.Row.RejectChanges() on both changing and changed event thats when i realised that e.Row is actually the proposed row. so Rejectchanges was giving me error "Cannot Canceledit() inside rowchanging event". But it was allowing reject to happen in RowChanged Event. so the following sequence happened: 1.RowChangingEvent e.Action=Added (as i was adding multiple rows to table) e.Row.RowState=Detached. 2. RowChanged Event e.Action=Added e.Row.RowState=Added (in Red as it was changed here and added to datatable)

As for the Exception : I recieved "Cannot cAll CancelEdit() in rowchanging event"

In rowChanged Event i got "This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row."

Please guide me with the above row changed Event Exception.

Upvotes: 0

Views: 1511

Answers (3)

Santosh Panda
Santosh Panda

Reputation: 7351

According to MSDN:

DataTable.RowChanging Event occurs when a DataRow is changing.

while

DataTable.RowChanged Event occurs after a DataRow has been changed successfully.

If you throw exception in changing event you can retire changes. Changed events you can use to make changes in other tables but you can't (in simple way) resign.

Yes, it True that both contain the same event data i.e. e.Action and e.Row; but it is more important that what operations you want to do at what point of time.

Means OnChanging event the data is not validated yet. So if you will do any code change at this stage then the data might not persist if validation gets failed. While in OnChanged event you can change the code as per your requirement.

Upvotes: 1

matzone
matzone

Reputation: 5719

From MSDN :

RowChanging : Occurs when a change has been submitted for a DataColumn value or the RowState of a DataRow in the DataTable.

RowChanged : Occurs after a DataColumn value or the RowState of a DataRow in the DataTable has been changed successfully. In this event we can tell to the app what will do next ..

(example here)

Upvotes: 0

Vahlkron
Vahlkron

Reputation: 464

The RowChanged Event occurs after a DataRow has been successfully changed.

The RowChanging Event occurs during the change of the DataRow. This will give you the opportunity to execute code before the row itself is changed.

Upvotes: 1

Related Questions