Reputation: 165
Say I have a DataTable like so:
state of D is DataViewRowState.Deleted and lets say O are rows that have not been touched
index: 0 | state: O
index: 1 | state: O
index: 2 | state: O
index: 3 | state: D
index: 4 | state: D
Say I delete the row at index 2 (I set the row state to deleted). Is there a way to know if row at index 1 is the last non-deleted row in the collection without "walking forward" and checking the rows state of each record after index 1?
Upvotes: 0
Views: 794
Reputation: 460238
You can use LINQ:
int deletedIndex = 2;
DataRow firstNonDeletedBeforeDeletedIndex = table.AsEnumerable()
.Where((row, index) => index < deletedIndex && row.RowState != DataRowState.Deleted)
.LastOrDefault(); // null if none found
Upvotes: 2