I want to know if the rows have changed values in my Datatable, how to check that? I got a Datatable has data and I make some changes on this data by grid view so Can I check which rows have changed.
CodePudding user response:
Each DataRow
has a RowState
. When you populate a DataTable
by calling Fill
on a data adapter, all rows start out Unchanged
. When you add a row the value will be Added
, when you edit an existing row the value will be Modified
and when you delete an existing row the value will be Deleted
.
Note that, when you call Fill
on a data adapter, the rows will be initially added with a RowState
of Added
but then AcceptChanges
is called and they will all be set to Unchanged
. If you set AcceptChangesDuringFill
to False
first, they will all remain Added
. This is useful when, for instance, you want to retrieve from one database and then insert into another.
When you call Update
on a data adapter to save changes, AcceptChanges
is called afterwards and all Added
and Modified
rows become Unchanged
and all Deleted
rows are removed. If you set AcceptChangesDuringUpdate
to False
first, they will all remain as they were. This is useful when, for instance, you want to save changes to multiple tables from one DataTable
. You would then call AcceptChanges
manually if you wanted to keep using the DataTable
.
Note that each DataRow
contains two versions of its data: original and current. When you get data from a field in a DataRow
you will get the current version by default, but you can specify which version you want. If the RowState
is Added
then the original version is empty and, if the the RowState
is Deleted
then the current version is empty. When AcceptChanges
is called, the current version is copied over the original version in rows that were Added
or Modified
.
You can also call GetChanges
on a DataTable
and that will either return Nothing
or a new DataTable
containing just the changed rows. You can also specify one or more DataRowState
values and get just changes of that type.